RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(ingestion, web): converge cancels fast and stop logging them as errors (#19992)
jay7772182b45422e082
diff --git a/internal/agent/canvas/node_body.go b/internal/agent/canvas/node_body.goindex d7fd6b791f..6f1c7808fa 100644--- a/internal/agent/canvas/node_body.go+++ b/internal/agent/canvas/node_body.go@@ -271,20 +271,30 @@ func realComponentBodyWithOptions(cpnID, componentClass string, comp runtime.Comreturn e})if invokeErr != nil {- // Surface the failure as a structured log line. The wrapped error- // already carries the full cause chain (e.g. deepseek DNS/timeout),- // but without this the failure only showed up as a generic- // "Task ... failed" line with no clear cause in the logs.- common.Error("canvas: component invoke failed", invokeErr,- zap.String("component_id", cpnID),- zap.String("component_class", componentClass))switch {- case errors.Is(invokeErr, context.DeadlineExceeded):- return nil, fmt.Errorf("canvas: component %q invoke: context deadline exceeded: %w", cpnID, invokeErr)case errors.Is(invokeErr, context.Canceled):+ // A user cancel is a normal control path; the authoritative+ // "Task ... cancelled" line is logged by the service layer, so+ // keep this at debug to avoid duplicate noise.+ common.Debug("canvas: component invoke cancelled",+ zap.String("component_id", cpnID),+ zap.String("component_class", componentClass))return nil, fmt.Errorf("canvas: component %q invoke: cancelled: %w", cpnID, invokeErr)+ case errors.Is(invokeErr, context.DeadlineExceeded):+ common.Error("canvas: component invoke failed", invokeErr,+ zap.String("component_id", cpnID),+ zap.String("component_class", componentClass))+ return nil, fmt.Errorf("canvas: component %q invoke: context deadline exceeded: %w", cpnID, invokeErr)+ default:+ // Surface the failure as a structured log line. The wrapped error+ // already carries the full cause chain (e.g. deepseek DNS/timeout),+ // but without this the failure only showed up as a generic+ // "Task ... failed" line with no clear cause in the logs.+ common.Error("canvas: component invoke failed", invokeErr,+ zap.String("component_id", cpnID),+ zap.String("component_class", componentClass))+ return nil, fmt.Errorf("canvas: component %q invoke: %w", cpnID, invokeErr)}- return nil, fmt.Errorf("canvas: component %q invoke: %w", cpnID, invokeErr)}if out == nil {out = make(map[string]any, 1)diff --git a/internal/deepdoc/parser/pdf/parser.go b/internal/deepdoc/parser/pdf/parser.goindex 215e548455..a5af099654 100644--- a/internal/deepdoc/parser/pdf/parser.go+++ b/internal/deepdoc/parser/pdf/parser.go@@ -2,6 +2,7 @@ package pdfimport ("context"+ "errors""fmt""image""math"@@ -463,7 +464,7 @@ func (p *Parser) assembleDocument(ctx context.Context, pages []int, pageResultsif r == nil {continue}- if r.Err != nil {+ if r.Err != nil && !errors.Is(r.Err, context.Canceled) {common.Warn("deepdoc pdf parse: page worker failed",zap.Int("page", r.PageNumber), zap.Error(r.Err))}@@ -647,7 +648,7 @@ func (p *Parser) processPages(ctx context.Context, engine pdf.PDFEngine, docAnal}pageResults, pageErr := p.runPageWorkers(ctx, engine, pages, docAnalyzer, tb)- if pageErr != nil {+ if pageErr != nil && !errors.Is(pageErr, context.Canceled) {common.Warn("deepdoc pdf parse: runPageWorkers some pages failed",zap.Error(pageErr))}diff --git a/internal/deepdoc/parser/pdf/parser_concurrency.go b/internal/deepdoc/parser/pdf/parser_concurrency.goindex 07cdc44f3d..a51a39a0ed 100644--- a/internal/deepdoc/parser/pdf/parser_concurrency.go+++ b/internal/deepdoc/parser/pdf/parser_concurrency.go@@ -6,6 +6,9 @@ import ("runtime""sync"+ "go.uber.org/zap"++ "ragflow/internal/common"pdf "ragflow/internal/deepdoc/parser/pdf/type""ragflow/internal/utility")@@ -169,6 +172,20 @@ func (p *Parser) inferDLA(ctx context.Context, doc pdf.DocAnalyzer, pageImg imagreturn doc.DLA(ctx, pageImg)}+// reportPageInferenceFailure logs one page-local inference failure (DLA, TSR or+// OCR). A failure raised while the parse context is cancelled is the stop path,+// not a fault: cancelling terminates every in-flight ONNX Run, and the native+// session answers with the runtime's terminate-flag error (or ctx.Err()), which+// carries no context.Canceled to match on. Those pages log at debug instead of+// warning once per page; any other failure keeps its per-page warning.+func reportPageInferenceFailure(ctx context.Context, msg string, page int, err error) {+ if ctx.Err() != nil {+ common.Debug(msg, zap.Int("page", page), zap.Error(err))+ return+ }+ common.Warn(msg, zap.Int("page", page), zap.Error(err))+}+// inferTSR invokes TSR for a single cropped table region.func (p *Parser) inferTSR(ctx context.Context, tb pdf.TableBuilder, cropped image.Image) ([]pdf.TSRCell, error) {if tb == nil {diff --git a/internal/deepdoc/parser/pdf/parser_ocr.go b/internal/deepdoc/parser/pdf/parser_ocr.goindex d342062436..b44d25c344 100644--- a/internal/deepdoc/parser/pdf/parser_ocr.go+++ b/internal/deepdoc/parser/pdf/parser_ocr.go@@ -16,7 +16,7 @@ func (p *Parser) ocrDetectAndRecognize(ctx context.Context, pageImg image.Image,boxes, err := p.inferOCRDetect(ctx, doc, pageImg)if err != nil || len(boxes) == 0 {if err != nil {- slog.Warn(logLabel+" OCR detect failed", "page", pageNum, "err", err)+ reportPageInferenceFailure(ctx, logLabel+" OCR detect failed", pageNum, err)}return nil}@@ -111,7 +111,7 @@ func (p *Parser) ocrDetectAndRecognize(ctx context.Context, pageImg image.Image,case berr != nil:// A batch error must not abort the whole page: the canonical// per-crop path below still produces correct results.- slog.Warn(logLabel+" OCR batch recognize failed; falling back to per-crop", "page", pageNum, "err", berr)+ reportPageInferenceFailure(ctx, logLabel+" OCR batch recognize failed; falling back to per-crop", pageNum, berr)case len(batch) != len(cropAcc):// Defensive: a count mismatch (or a nil result) would corrupt the// per-box indexing further down. Fall back to per-crop instead of@@ -134,7 +134,7 @@ func (p *Parser) ocrDetectAndRecognize(ctx context.Context, pageImg image.Image,recCtx := context.WithValue(ctx, ocrBoxIdxCtxKey, cropBoxIdx[ci])texts, rerr := p.ocrRecognizeWithRotation(recCtx, doc, c)if rerr != nil {- slog.Warn(logLabel+" OCR recognize failed", "page", pageNum, "err", rerr)+ reportPageInferenceFailure(recCtx, logLabel+" OCR recognize failed", pageNum, rerr)return nil}allTexts[ci] = texts@@ -581,7 +581,7 @@ func (p *Parser) buildTextBoxes(ctx context.Context, pageImg image.Image,if p.docSupportsBatchOCR(doc) {batch, berr := p.inferOCRRecognizeBatch(ctx, doc, crops)if berr != nil {- slog.Warn("ocr merge: batch recognize failed", "page", pageNum, "err", berr)+ reportPageInferenceFailure(ctx, "ocr merge: batch recognize failed", pageNum, berr)return nil}allTexts = batch@@ -594,7 +594,7 @@ func (p *Parser) buildTextBoxes(ctx context.Context, pageImg image.Image,recCtx := context.WithValue(ctx, ocrBoxIdxCtxKey, jobs[ci].srcIdx)texts, rerr := p.ocrRecognizeWithRotation(recCtx, doc, c)if rerr != nil {- slog.Warn("ocr merge: recognize failed", "page", pageNum, "err", rerr)+ reportPageInferenceFailure(recCtx, "ocr merge: recognize failed", pageNum, rerr)continue}allTexts[ci] = textsdiff --git a/internal/deepdoc/parser/pdf/parser_parallel_test.go b/internal/deepdoc/parser/pdf/parser_parallel_test.goindex a6b9253fd0..729ca0ea9b 100644--- a/internal/deepdoc/parser/pdf/parser_parallel_test.go+++ b/internal/deepdoc/parser/pdf/parser_parallel_test.go@@ -5,6 +5,7 @@ import ("context""crypto/sha256""encoding/hex"+ "errors""image""image/png""reflect"@@ -12,6 +13,11 @@ import ("sync""testing"+ "go.uber.org/zap"+ "go.uber.org/zap/zapcore"+ "go.uber.org/zap/zaptest/observer"++ "ragflow/internal/common"pdf "ragflow/internal/deepdoc/parser/pdf/type")@@ -313,6 +319,41 @@ func TestParser_RunPageWorkers_CancellationHonored(t *testing.T) {}}+// TestReportPageInferenceFailure_CancelledContextStaysAtDebug verifies the+// per-page inference logger distinguishes a stop from a fault. Cancelling a run+// terminates every in-flight ONNX Run, whose error (the runtime's terminate-flag+// text, or ctx.Err()) must not produce one warning per page — it keeps a debug+// trail instead; a failure raised on a live context still warns.+func TestReportPageInferenceFailure_CancelledContextStaysAtDebug(t *testing.T) {+ prevLogger := common.Logger+ defer func() { common.Logger = prevLogger }()+ core, logs := observer.New(zapcore.DebugLevel)+ common.Logger = zap.New(core)++ ctx, cancel := context.WithCancel(context.Background())+ cancel()+ reportPageInferenceFailure(ctx, "DLA failed", 7,+ errors.New("Error running network: Exiting due to terminate flag being set to true."))+ entries := logs.TakeAll()+ if len(entries) != 1 || entries[0].Level != zapcore.DebugLevel ||+ entries[0].Message != "DLA failed" {+ t.Fatalf("cancelled page inference failure left no debug trail: %+v", entries)+ }+ if got := entries[0].ContextMap()["page"]; got != int64(7) {+ t.Fatalf("debug entry lost the page number: %v", got)+ }++ reportPageInferenceFailure(context.Background(), "DLA failed", 7, errors.New("output shape mismatch"))+ entries = logs.TakeAll()+ if len(entries) != 1 || entries[0].Level != zapcore.WarnLevel ||+ entries[0].Message != "DLA failed" {+ t.Fatalf("live page inference failure was not warned: %+v", entries)+ }+ if got := entries[0].ContextMap()["page"]; got != int64(7) {+ t.Fatalf("warning lost the page number: %v", got)+ }+}+// TestParser_ProcessPages_CrossPageTableMerge verifies that cross-page// tables still merge after being collected from per-page workers, even// when different workers process the participating pages. This is thediff --git a/internal/deepdoc/parser/pdf/table_extract.go b/internal/deepdoc/parser/pdf/table_extract.goindex c501fe2141..ebeedd70c2 100644--- a/internal/deepdoc/parser/pdf/table_extract.go+++ b/internal/deepdoc/parser/pdf/table_extract.go@@ -80,7 +80,7 @@ func (p *Parser) enrichOnePageWithDeepDoc(ctx context.Context,ctx = context.WithValue(ctx, pageNumCtxKey, pg)regions, err := p.inferDLA(ctx, docAnalyzer, pageImg)if err != nil {- slog.Warn("DLA failed", "page", pg, "err", err)+ reportPageInferenceFailure(ctx, "DLA failed", pg, err)return pageBoxes, nil, nil}dlaRegions = []pdf.DLAPageRegions{{Page: pg, Regions: regions}}@@ -135,7 +135,7 @@ func (p *Parser) processOneTable(ctx context.Context, pageImg image.Image, boxestsrCtx = context.WithValue(tsrCtx, cropOffYKey, cropOffY)cells, tsrErr := p.inferTSR(tsrCtx, tb, tsrImg)if tsrErr != nil {- slog.Warn("TSR failed", "page", pageNum, "err", tsrErr)+ reportPageInferenceFailure(tsrCtx, "TSR failed", pageNum, tsrErr)}var boxInCrop []pdf.TextBoxif tsrErr == nil && len(cells) > 0 {diff --git a/internal/ingestion/pipeline/pipeline.go b/internal/ingestion/pipeline/pipeline.goindex 1551d64c45..11882767e3 100644--- a/internal/ingestion/pipeline/pipeline.go+++ b/internal/ingestion/pipeline/pipeline.go@@ -355,7 +355,15 @@ func (p *Pipeline) componentProgressCallback(ctx context.Context) runtime.Progre// higher-level "Task ... failed" branch.switch ev.Phase {case runtime.PhaseError:- if ev.Err != nil {+ if errors.Is(ev.Err, context.Canceled) {+ // A user cancel is a normal control path; the authoritative+ // "Task ... cancelled" line is logged by the service layer, so+ // keep this at debug to avoid duplicate noise.+ common.Debug("component progress: canceled",+ zap.String("component", ev.Component),+ zap.String("task_id", p.taskID),+ zap.String("document_id", p.documentID))+ } else if ev.Err != nil {common.Error("component progress: error", ev.Err,zap.String("component", ev.Component),zap.String("task_id", p.taskID),diff --git a/internal/ingestion/service/ingestion_service.go b/internal/ingestion/service/ingestion_service.goindex 8e885750a8..89c4aaddef 100644--- a/internal/ingestion/service/ingestion_service.go+++ b/internal/ingestion/service/ingestion_service.go@@ -130,11 +130,12 @@ type Ingestor struct {// may replace it to verify lifecycle behavior without a database or broker.reconcileMemoryTasks func(ctx context.Context) error- // cancelCheck is polled periodically (every 3s) during task execution.- // When it returns true the task's context is cancelled, which causes the- // pipeline to stop at the next ctx.Err() check. Defaults to a Redis- // cancel-flag lookup that mirrors Python's has_canceled(). Tests may- // override this to simulate cancel without Redis.+ // cancelCheck is polled periodically (pollCancelInterval) during task+ // execution. When it returns true the task's context is cancelled, which+ // causes the pipeline to stop at the next ctx.Err() check. Defaults to a+ // Redis cancel-flag lookup that mirrors Python's has_canceled(), with a+ // DB fallback when Redis is unavailable. Tests may override this to+ // simulate cancel without Redis.cancelCheck func(ctx context.Context, taskID string) bool}@@ -1049,10 +1050,17 @@ func (e *Ingestor) defaultCancelCheck(ctx context.Context, taskID string) bool {return task.Status == common.STOPPING}-// pollCancel ticks every 3s to check the cancel flag. When cancelCheck+const pollCancelInterval = 500 * time.Millisecond++// pollCancel ticks every pollCancelInterval to check the cancel flag. When cancelCheck// returns true it cancels the per-task context, which causes the pipeline's// next ctx.Err() check to abort and runTask to record progress=-1. The// goroutine exits when done is closed (executeTask returns).+//+// Cost note: without Redis in the deployment, every tick falls through to the+// DB fallback in defaultCancelCheck — two GetTask reads per second per running+// task. That is the accepted trade-off for fast cancel detection; with Redis+// configured the hot path is a single EXISTS lookup.func (e *Ingestor) pollCancel(taskID string, cancel context.CancelFunc, done <-chan struct{}) {// checkOnce runs cancelCheck in a goroutine so the caller can select// between the result and the done signal. This prevents a blocked@@ -1079,7 +1087,7 @@ func (e *Ingestor) pollCancel(taskID string, cancel context.CancelFunc, done <-c}}- ticker := time.NewTicker(3 * time.Second)+ ticker := time.NewTicker(pollCancelInterval)defer ticker.Stop()for {select {diff --git a/internal/ingestion/service/progress_sink.go b/internal/ingestion/service/progress_sink.goindex bef6f6fba0..8e8ad8304a 100644--- a/internal/ingestion/service/progress_sink.go+++ b/internal/ingestion/service/progress_sink.go@@ -18,6 +18,7 @@ package serviceimport ("context"+ "errors""fmt""sync""time"@@ -135,6 +136,14 @@ func (s *progressSink) flush(ctx context.Context, force bool) {return}if err := s.docSvc.UpdateRunState(ctx, docID, p); err != nil {+ // A stop tears the run context down while a periodic flush is in+ // flight, aborting that UPDATE. It is the normal cancel path, not a+ // persistence failure: the final flush in Close detaches from the+ // cancelled context and still writes the last percent.+ if errors.Is(err, context.Canceled) {+ common.Debug(fmt.Sprintf("progressSink: flush progress for document %s aborted: %v", docID, err))+ return+ }common.Warn(fmt.Sprintf("progressSink: flush progress for document %s failed: %v", docID, err))return}diff --git a/internal/ingestion/service/progress_sink_test.go b/internal/ingestion/service/progress_sink_test.goindex 17c7f4a2e9..f8213cc998 100644--- a/internal/ingestion/service/progress_sink_test.go+++ b/internal/ingestion/service/progress_sink_test.go@@ -18,11 +18,18 @@ package serviceimport ("context"+ "database/sql"+ "errors""runtime""sync""testing""time"+ "go.uber.org/zap"+ "go.uber.org/zap/zapcore"+ "go.uber.org/zap/zaptest/observer"++ "ragflow/internal/common""ragflow/internal/dao""ragflow/internal/ingestion/pipeline""ragflow/internal/ingestion/testutil"@@ -408,3 +415,64 @@ func TestProgressSinkCloseFlushSurvivesCancelledRunContext(t *testing.T) {t.Fatalf("final flush = calls:%d doc:%q progress:%v, want 1/%q/0.5 (ticker flushes must fail on cancelled ctx, Close flush must succeed)", calls, gotDocID, gotProgress, docID)}}++// failingDocProgressSvc fails every mirror write, so tests can drive the+// sink's flush-error branch deterministically.+type failingDocProgressSvc struct {+ err error+ attempts int+}++func (s *failingDocProgressSvc) UpdateRunState(_ context.Context, _ string, _ float64) error {+ s.attempts+++ return s.err+}++// TestProgressSinkFlushCancellationDoesNotWarn pins the log level of the+// periodic-flush error branch. A stop cancels the run context underneath an+// in-flight UPDATE, so the call comes back as a cancellation (GORM joins+// ctx.Err() with sql.ErrTxDone, hence the errors.Is check - the reported error+// is not context.Canceled itself). That abort is the normal stop path and must+// stay at debug; a genuine write failure must still warn.+func TestProgressSinkFlushCancellationDoesNotWarn(t *testing.T) {+ db := testutil.SetupTestDB(t)+ cleanup := testutil.ReplaceDBForTest(t, db)+ defer cleanup()++ prevLogger := common.Logger+ defer func() { common.Logger = prevLogger }()+ core, logs := observer.New(zapcore.WarnLevel)+ common.Logger = zap.New(core)++ ctx := t.Context()+ sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService(), "run-1")+ defer sink.Close()+ sink.bindDocument("doc-1")++ // Cancellation, in the joined shape a cancelled DB call produces.+ cancelStub := &failingDocProgressSvc{+ err: errors.Join(context.Canceled, sql.ErrTxDone),+ }+ sink.docSvc = cancelStub+ sink.flush(ctx, true)+ if cancelStub.attempts != 1 {+ t.Fatalf("flush attempts = %d, want 1 (the branch under test must actually run)", cancelStub.attempts)+ }+ if logs.Len() != 0 {+ t.Fatalf("cancelled flush logged %d warn entries: %v", logs.Len(), logs.All())+ }++ // A real failure keeps warning.+ failStub := &failingDocProgressSvc{err: errors.New("db down")}+ sink.docSvc = failStub+ sink.flush(ctx, true)+ if failStub.attempts != 1 {+ t.Fatalf("flush attempts = %d, want 1", failStub.attempts)+ }+ if logs.Len() != 1 {+ t.Fatalf("write failure logged %d warn entries, want 1", logs.Len())+ }+ if entry := logs.All()[0]; entry.Level != zapcore.WarnLevel {+ t.Fatalf("write failure logged at %v, want warn", entry.Level)+ }+}diff --git a/internal/service/ingestion_task_service.go b/internal/service/ingestion_task_service.goindex 0cb07dae14..a4483561f1 100644--- a/internal/service/ingestion_task_service.go+++ b/internal/service/ingestion_task_service.go@@ -318,8 +318,8 @@ func (s *IngestionTaskService) RequestStop(ctx context.Context, taskID string) (return nil, err}// Mirror Python's cancel_all_task_of: set Redis cancel flag so the- // running worker's pollCancel detects the stop immediately rather- // than waiting for the next DB poll (up to 3s).+ // running worker's pollCancel detects the stop on its next 500ms+ // tick instead of the slower DB fallback path.if rc := kvrocks.Get(); rc != nil {rc.Set(ctx, fmt.Sprintf("%s-cancel", taskID), "x", 1*time.Hour)}diff --git a/web/src/hooks/__tests__/cancel-stop-loss.test.ts b/web/src/hooks/__tests__/cancel-stop-loss.test.tsnew file mode 100644index 0000000000..3e61c3ed33--- /dev/null+++ b/web/src/hooks/__tests__/cancel-stop-loss.test.ts@@ -0,0 +1,169 @@+/*+ * Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import {+ CANCEL_STOP_LOSS_MS,+ getCancelRequestInterval,+ markCancelRequested,+ observeStoppingDocuments,+} from '../cancel-stop-loss';++const advance = (ms: number) => jest.advanceTimersByTime(ms);++describe('cancel stop-loss', () => {+ // Trackers live for the lifetime of the page and are not reset between+ // tests, so every test uses its own document ids.+ beforeEach(() => {+ jest.useFakeTimers();+ });++ afterEach(() => {+ jest.useRealTimers();+ });++ it('keeps the fast interval while the cancel is fresh', () => {+ markCancelRequested(['doc-fresh']);++ expect(getCancelRequestInterval(['doc-fresh'])).toBe(500);++ advance(CANCEL_STOP_LOSS_MS - 1);++ expect(getCancelRequestInterval(['doc-fresh'])).toBe(500);+ });++ it('falls back to the 5s interval once the cancel is overdue', () => {+ markCancelRequested(['doc-overdue']);++ advance(CANCEL_STOP_LOSS_MS);++ expect(getCancelRequestInterval(['doc-overdue'])).toBe(5000);+ });++ it('keeps the fast interval while any stopping document is still fresh', () => {+ markCancelRequested(['doc-mixed-old']);++ advance(CANCEL_STOP_LOSS_MS);+ markCancelRequested(['doc-mixed-new']);++ expect(getCancelRequestInterval(['doc-mixed-old', 'doc-mixed-new'])).toBe(+ 500,+ );+ });++ it('reports an overdue cancel once, then latches the retry', () => {+ markCancelRequested(['doc-latched']);++ expect(observeStoppingDocuments(['doc-latched'], ['doc-latched'])).toEqual(+ [],+ );++ advance(CANCEL_STOP_LOSS_MS);++ expect(observeStoppingDocuments(['doc-latched'], ['doc-latched'])).toEqual([+ 'doc-latched',+ ]);+ expect(observeStoppingDocuments(['doc-latched'], ['doc-latched'])).toEqual(+ [],+ );+ });++ it('retries a cancel again only after the user re-arms it', () => {+ markCancelRequested(['doc-rearmed']);++ advance(CANCEL_STOP_LOSS_MS);+ expect(observeStoppingDocuments(['doc-rearmed'], ['doc-rearmed'])).toEqual([+ 'doc-rearmed',+ ]);+ advance(CANCEL_STOP_LOSS_MS);+ expect(observeStoppingDocuments(['doc-rearmed'], ['doc-rearmed'])).toEqual(+ [],+ );++ markCancelRequested(['doc-rearmed']);+ advance(CANCEL_STOP_LOSS_MS);++ expect(observeStoppingDocuments(['doc-rearmed'], ['doc-rearmed'])).toEqual([+ 'doc-rearmed',+ ]);+ });++ it('never retries a stopping document first seen without a local cancel', () => {+ // Page reload: the document is already stopping, nothing was clicked here.+ expect(observeStoppingDocuments(['doc-adopted'], ['doc-adopted'])).toEqual(+ [],+ );++ // The window starts at the first sight, so polling stays fast ...+ expect(getCancelRequestInterval(['doc-adopted'])).toBe(500);++ // ... then downgrades once the window has elapsed, and never retries.+ advance(CANCEL_STOP_LOSS_MS);++ expect(getCancelRequestInterval(['doc-adopted'])).toBe(5000);+ expect(observeStoppingDocuments(['doc-adopted'], ['doc-adopted'])).toEqual(+ [],+ );+ });++ it('only collects the overdue ids out of a mixed batch', () => {+ markCancelRequested(['doc-old']);++ advance(CANCEL_STOP_LOSS_MS);+ markCancelRequested(['doc-new']);++ expect(+ observeStoppingDocuments(['doc-old', 'doc-new'], ['doc-old', 'doc-new']),+ ).toEqual(['doc-old']);++ advance(CANCEL_STOP_LOSS_MS);++ expect(+ observeStoppingDocuments(['doc-old', 'doc-new'], ['doc-old', 'doc-new']),+ ).toEqual(['doc-new']);+ });++ it('drops tracking once a document is observed to leave the stopping state', () => {+ markCancelRequested(['doc-resumed']);++ advance(CANCEL_STOP_LOSS_MS);+ // Observed as running again: the cancel is over, drop its trackers.+ observeStoppingDocuments(['doc-resumed'], []);++ // Back to stopping later: treated as a fresh observation, not a retry of+ // the old cancel.+ expect(observeStoppingDocuments(['doc-resumed'], ['doc-resumed'])).toEqual(+ [],+ );+ expect(getCancelRequestInterval(['doc-resumed'])).toBe(500);+ });++ it('keeps tracking a cancel on a document missing from the current result', () => {+ markCancelRequested(['doc-offpage']);++ // The list moved to another page / search / filter: the document is not+ // part of the current result at all.+ advance(CANCEL_STOP_LOSS_MS - 1);+ observeStoppingDocuments(['doc-elsewhere'], ['doc-elsewhere']);++ // Once it shows up again still stopping, the cancel is overdue and the+ // retry was preserved.+ advance(1);++ expect(observeStoppingDocuments(['doc-offpage'], ['doc-offpage'])).toEqual([+ 'doc-offpage',+ ]);+ });+});diff --git a/web/src/hooks/__tests__/document-ingest-in-flight.test.ts b/web/src/hooks/__tests__/document-ingest-in-flight.test.tsnew file mode 100644index 0000000000..5eb24be18f--- /dev/null+++ b/web/src/hooks/__tests__/document-ingest-in-flight.test.ts@@ -0,0 +1,104 @@+/*+ * Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { sendDocumentIngest } from '../document-ingest-in-flight';++describe('sendDocumentIngest', () => {+ it('shares the request between identical calls regardless of id order', async () => {+ const request = jest.fn(() => Promise.resolve('done'));++ const first = sendDocumentIngest(+ { documentIds: ['doc-shared', 'doc-other'], run: 2 },+ request,+ );+ const second = sendDocumentIngest(+ { documentIds: ['doc-other', 'doc-shared'], run: 2 },+ request,+ );++ expect(request).toHaveBeenCalledTimes(1);+ expect(second).toBe(first);+ await expect(first).resolves.toBe('done');+ });++ it('does not reuse the entry of a settled request', async () => {+ const params = { documentIds: ['doc-settled'], run: 2 };+ await sendDocumentIngest(params, () => Promise.resolve('done'));++ const request = jest.fn(() => Promise.resolve('done'));+ await sendDocumentIngest(params, request);++ expect(request).toHaveBeenCalledTimes(1);+ });++ it('issues a new request for a forced call while an identical one is pending', async () => {+ let resolveFirst!: (value: string) => void;+ let resolveRetry!: (value: string) => void;+ const params = { documentIds: ['doc-forced'], run: 2 };+ const first = sendDocumentIngest(+ params,+ () => new Promise<string>((resolve) => (resolveFirst = resolve)),+ );++ const request = jest.fn(+ () => new Promise<string>((resolve) => (resolveRetry = resolve)),+ );+ const retry = sendDocumentIngest(params, request, { force: true });++ expect(request).toHaveBeenCalledTimes(1);+ expect(retry).not.toBe(first);++ // Settling the original request must not evict the pending retry from+ // the key.+ resolveFirst('first');+ await first;++ const third = sendDocumentIngest(params, () => Promise.resolve('third'));+ expect(third).toBe(retry);++ resolveRetry('retry');+ await expect(retry).resolves.toBe('retry');+ });++ it('starts a fresh request when the retry settles before the original', async () => {+ let resolveFirst!: (value: string) => void;+ let resolveRetry!: (value: string) => void;+ const params = { documentIds: ['doc-reverse'], run: 2 };+ const first = sendDocumentIngest(+ params,+ () => new Promise<string>((resolve) => (resolveFirst = resolve)),+ );++ const request = jest.fn(+ () => new Promise<string>((resolve) => (resolveRetry = resolve)),+ );+ const retry = sendDocumentIngest(params, request, { force: true });++ // The retry settles first; it must free the key even though the original+ // is still pending, so the next call issues a fresh request instead of+ // being served by the stale original.+ resolveRetry('retry');+ await expect(retry).resolves.toBe('retry');++ const third = sendDocumentIngest(params, () => Promise.resolve('third'));+ expect(request).toHaveBeenCalledTimes(1);+ expect(third).not.toBe(first);+ await expect(third).resolves.toBe('third');++ resolveFirst('first');+ await expect(first).resolves.toBe('first');+ });+});diff --git a/web/src/hooks/__tests__/use-document-request-cancel-chain.test.tsx b/web/src/hooks/__tests__/use-document-request-cancel-chain.test.tsxnew file mode 100644index 0000000000..630122efc7--- /dev/null+++ b/web/src/hooks/__tests__/use-document-request-cancel-chain.test.tsx@@ -0,0 +1,292 @@+/*+ * Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { IngestionTaskStatus } from '@/constants/knowledge';+import kbService, { listDocument } from '@/services/knowledge-service';+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';+import { act, renderHook, waitFor } from '@testing-library/react';+import React from 'react';++jest.mock('@/services/knowledge-service', () => ({+ __esModule: true,+ default: { documentIngest: jest.fn() },+ listDocument: jest.fn(),+}));++jest.mock('@/components/list-filter-bar/use-handle-filter-submit', () => ({+ useHandleFilterSubmit: () => ({+ filterValue: {},+ handleFilterSubmit: jest.fn(),+ checkValue: jest.fn(),+ }),+}));++jest.mock('@/components/ui/message', () => ({+ __esModule: true,+ default: { success: jest.fn(), error: jest.fn() },+}));++jest.mock('@/locales/config', () => ({+ __esModule: true,+ default: { t: (key: string) => key },+}));++// `pickByBackend` keeps the real dataset predicates on the Go branch, which is+// what the list's polling and the stop-loss read.+jest.mock('@/utils/backend-variant', () => ({+ useIsGoBackend: () => true,+ pickByBackend: ({ go }: { go: unknown }) => go,+}));++jest.mock('@/hooks/logic-hooks', () => ({+ useGetPaginationWithRouter: () => ({+ pagination: { current: 1, pageSize: 30, total: 0 },+ setPagination: jest.fn(),+ }),+ useHandleSearchChange: () => ({+ searchString: '',+ handleInputChange: jest.fn(),+ }),+}));++jest.mock('@/hooks/route-hook', () => ({+ useGetKnowledgeSearchParams: () => ({ knowledgeId: 'kb-chain' }),+ useSetPaginationParams: () => ({+ setPaginationParams: jest.fn(),+ page: 1,+ size: 30,+ }),+}));++jest.mock('react-router', () => ({+ useParams: () => ({}),+}));++jest.mock('ahooks', () => ({+ useDebounce: (value: unknown) => value,+}));++// Avoids the module cycle back into use-document-request.+jest.mock('@/pages/dataset/dataset/use-select-filters', () => ({+ EMPTY_METADATA_FIELD: 'empty_metadata',+}));++import { useFetchDocumentList, useRunDocument } from '../use-document-request';++const mockList = jest.mocked(listDocument);+const mockIngest = jest.mocked(kbService.documentIngest);++// Declared locally: an imported binding used in a type position breaks the+// babel pass that hoists jest.mock, and the list only reads these fields.+interface ChainDocument {+ id: string;+ name: string;+ ingestion_status: string;+}++const makeDoc = (id: string, status: string) =>+ ({+ id,+ name: `${id}.pdf`,+ ingestion_status: status,+ }) as unknown as ChainDocument;++const listResult = (doc: ChainDocument) => ({+ data: { code: 0, data: { docs: [doc], total: 1 } },+});++// Drives the same pair of hooks the dataset list page uses, so the cancel+// click, the optimistic update, the poll cadence and the stop-loss all run+// through the real React Query runtime.+const useCancelChain = () => ({+ list: useFetchDocumentList(true),+ run: useRunDocument(),+});++function makeWrapper() {+ const queryClient = new QueryClient({+ defaultOptions: {+ queries: { retry: false },+ mutations: { retry: false },+ },+ });+ // esbuild-jest loads .tsx with the plain "ts" loader here, so JSX in this+ // test file would not transform. Build the provider element with+ // createElement instead.+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ const Wrapper = (props: { children: any }) =>+ React.createElement(+ QueryClientProvider,+ { client: queryClient },+ props.children,+ );+ return Wrapper;+}++// Steps must be finer than the poll interval: React Query skips an interval+// tick while a request is in flight, and a synchronous advance does not flush+// microtasks, so a coarse step would collapse every interval into one request+// and hide the poll cadence under test.+const advanceSeconds = async (seconds: number) => {+ for (let i = 0; i < seconds * 4; i++) {+ await act(async () => {+ jest.advanceTimersByTime(250);+ });+ }+};++describe('document cancel chain (list polling + stop-loss retry)', () => {+ beforeEach(() => {+ jest.clearAllMocks();+ jest.useFakeTimers();+ });++ afterEach(() => {+ jest.useRealTimers();+ });++ it('keeps polling fast after the click and re-sends the cancel once it is overdue', async () => {+ const docId = 'doc-chain-overdue';+ mockList.mockResolvedValueOnce(+ listResult(makeDoc(docId, IngestionTaskStatus.RUNNING)) as never,+ );+ mockList.mockResolvedValue(+ listResult(makeDoc(docId, IngestionTaskStatus.STOPPING)) as never,+ );+ // The cancel request hangs (a 100+ page parse can hold it for minutes),+ // which is exactly the state the stop-loss has to survive.+ mockIngest.mockReturnValue(new Promise(() => {}) as never);++ const { result } = renderHook(useCancelChain, { wrapper: makeWrapper() });++ await waitFor(() => expect(result.current.list.documents).toHaveLength(1));+ expect(result.current.list.documents[0].ingestion_status).toBe(+ IngestionTaskStatus.RUNNING,+ );++ await act(async () => {+ void result.current.run.runDocumentByIds({+ documentIds: [docId],+ run: 2,+ });+ });++ // Optimistic STOPPING lands immediately, so the row leaves its idle state+ // before the server has answered.+ await waitFor(() =>+ expect(result.current.list.documents[0].ingestion_status).toBe(+ IngestionTaskStatus.STOPPING,+ ),+ );+ await waitFor(() => expect(mockIngest).toHaveBeenCalledTimes(1));+ expect(mockIngest).toHaveBeenCalledWith({ doc_ids: [docId], run: 2 });++ await advanceSeconds(35);++ // Fast polling for the whole window, then exactly one forced retry that+ // reaches the server while the first request is still pending. Every poll+ // returns a byte-identical stopping row (the stuck case), so the retry must+ // not depend on the list data changing between polls.+ expect(mockList.mock.calls.length).toBeGreaterThanOrEqual(55);+ expect(mockIngest).toHaveBeenCalledTimes(2);+ expect(mockIngest).toHaveBeenLastCalledWith({ doc_ids: [docId], run: 2 });++ // The retry is latched: the overdue window must not produce a request+ // storm, and the poll falls back to the slow cadence.+ const callsAtRetry = mockList.mock.calls.length;+ await advanceSeconds(15);+ expect(mockIngest).toHaveBeenCalledTimes(2);+ expect(mockList.mock.calls.length).toBeLessThan(callsAtRetry + 6);+ });++ it('polls an adopted STOPPING document fast, then slows down without ever re-sending', async () => {+ const docId = 'doc-chain-adopted';+ mockList.mockResolvedValue(+ listResult(makeDoc(docId, IngestionTaskStatus.STOPPING)) as never,+ );++ const { result } = renderHook(useCancelChain, { wrapper: makeWrapper() });++ await waitFor(() => expect(result.current.list.documents).toHaveLength(1));++ // A page reload adopts a cancel nobody clicked here: polling stays fast+ // for the window ...+ await advanceSeconds(5);+ expect(mockList.mock.calls.length).toBeGreaterThanOrEqual(8);++ // ... and after the window the interval downgrades instead of hammering+ // the list endpoint forever.+ await advanceSeconds(30);+ const callsAtWindowEnd = mockList.mock.calls.length;+ await advanceSeconds(15);+ expect(mockList.mock.calls.length).toBeLessThan(callsAtWindowEnd + 6);++ expect(mockIngest).not.toHaveBeenCalled();+ });++ it('keeps the optimistic STOPPING when a pre-cancel list response lands late', async () => {+ const docId = 'doc-chain-stale';+ let resolveStalePoll!: (value: unknown) => void;+ const stalePoll = new Promise((resolve) => {+ resolveStalePoll = resolve;+ });++ mockList.mockResolvedValueOnce(+ listResult(makeDoc(docId, IngestionTaskStatus.RUNNING)) as never,+ );+ // The poll that is in flight when the click lands answers with the+ // pre-cancel RUNNING row, held back until after the optimistic update.+ mockList.mockReturnValueOnce(stalePoll as never);+ mockList.mockReturnValue(new Promise(() => {}) as never);+ mockIngest.mockReturnValue(new Promise(() => {}) as never);++ const { result } = renderHook(useCancelChain, { wrapper: makeWrapper() });++ await waitFor(() => expect(result.current.list.documents).toHaveLength(1));+ expect(result.current.list.documents[0].ingestion_status).toBe(+ IngestionTaskStatus.RUNNING,+ );++ // RUNNING polls every 5s: let one request go out and stay pending.+ await advanceSeconds(8);+ expect(mockList.mock.calls.length).toBeGreaterThanOrEqual(2);++ await act(async () => {+ void result.current.run.runDocumentByIds({+ documentIds: [docId],+ run: 2,+ });+ });++ await waitFor(() =>+ expect(result.current.list.documents[0].ingestion_status).toBe(+ IngestionTaskStatus.STOPPING,+ ),+ );++ // Without dropping the in-flight refetch, this stale response would+ // overwrite STOPPING, reset the stop-loss tracking and leave the row+ // looking running again until the next poll contradicts it.+ await act(async () => {+ resolveStalePoll(listResult(makeDoc(docId, IngestionTaskStatus.RUNNING)));+ });+ await advanceSeconds(1);++ expect(result.current.list.documents[0].ingestion_status).toBe(+ IngestionTaskStatus.STOPPING,+ );+ });+});diff --git a/web/src/hooks/cancel-stop-loss.ts b/web/src/hooks/cancel-stop-loss.tsnew file mode 100644index 0000000000..f50caf7604--- /dev/null+++ b/web/src/hooks/cancel-stop-loss.ts@@ -0,0 +1,97 @@+/*+ * Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++// Stop-loss for cancels that never converge: a stuck STOPPING status must not+// keep the document list on the fast poll interval forever. Once a cancel has+// been pending for CANCEL_STOP_LOSS_MS the list falls back to the normal+// cadence and one cancel request is re-sent for the overdue documents.+export const CANCEL_STOP_LOSS_MS = 30_000;++// Cadence while a cancel is within its window: the ingestor polls its own+// cancel flag every 500ms, so 500ms here converges the row on the first list+// response after the worker settles instead of waiting out a longer poll.+export const CANCEL_POLL_MS = 500;++// Window start per document: set when the user cancels, or at the first sight+// of a stopping document that predates this session (e.g. a page reload), so+// an orphaned cancel still downgrades once its window has elapsed.+const cancelRequestedAt = new Map<string, number>();+// Only cancels issued by this session are retried; adopted ones are not.+const userRequestedCancelIds = new Set<string>();+// Latch: a retry is sent at most once per cancel request.+const cancelRetrySent = new Set<string>();++// Re-arming an id resets its window and clears the retry latch, so a new+// cancel click starts from a clean slate.+export const markCancelRequested = (documentIds: string[]) => {+ const now = Date.now();+ documentIds.forEach((id) => {+ cancelRequestedAt.set(id, now);+ userRequestedCancelIds.add(id);+ cancelRetrySent.delete(id);+ });+};++export const getCancelRequestInterval = (stoppingIds: string[]) => {+ const now = Date.now();+ // An untracked id counts as freshly observed, so a stopping document that+ // first appears here still gets a full window of fast polling.+ return stoppingIds.some(+ (id) => now - (cancelRequestedAt.get(id) ?? now) < CANCEL_STOP_LOSS_MS,+ )+ ? CANCEL_POLL_MS+ : 5000;+};++// Called on every list poll with the ids the poll observed and the subset+// still stopping: starts the window for ids first seen here, drops trackers+// for observed ids that left the stopping state, and returns the overdue+// cancels whose retry has not been sent yet. Ids missing from the observed+// set (another page, a search or a filter) keep their trackers, so their+// retry is still there when the document shows up again.+export const observeStoppingDocuments = (+ observedIds: string[],+ stoppingIds: string[],+) => {+ const now = Date.now();+ const observedIdSet = new Set(observedIds);+ const stoppingIdSet = new Set(stoppingIds);++ cancelRequestedAt.forEach((_, id) => {+ if (observedIdSet.has(id) && !stoppingIdSet.has(id)) {+ cancelRequestedAt.delete(id);+ userRequestedCancelIds.delete(id);+ cancelRetrySent.delete(id);+ }+ });++ return stoppingIds.filter((id) => {+ const requestedAt = cancelRequestedAt.get(id);+ if (requestedAt === undefined) {+ cancelRequestedAt.set(id, now);+ return false;+ }+ if (+ !userRequestedCancelIds.has(id) ||+ cancelRetrySent.has(id) ||+ now - requestedAt < CANCEL_STOP_LOSS_MS+ ) {+ return false;+ }+ cancelRetrySent.add(id);+ return true;+ });+};diff --git a/web/src/hooks/document-ingest-in-flight.ts b/web/src/hooks/document-ingest-in-flight.tsnew file mode 100644index 0000000000..99bb449ed1--- /dev/null+++ b/web/src/hooks/document-ingest-in-flight.ts@@ -0,0 +1,51 @@+/*+ * Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++const documentIngestInFlight = new Map<string, Promise<unknown>>();++export const sendDocumentIngest = (+ params: {+ documentIds: string[];+ run: number;+ option?: { delete: boolean; apply_kb: boolean };+ },+ request: () => Promise<unknown>,+ { force = false }: { force?: boolean } = {},+) => {+ const key = JSON.stringify({+ documentIds: [...params.documentIds].sort(),+ run: params.run,+ option: params.option || null,+ });+ // The stop-loss retry must reach the server even while an identical request+ // is still pending, so it bypasses the in-flight entry.+ if (!force) {+ const existingRequest = documentIngestInFlight.get(key);+ if (existingRequest) {+ return existingRequest;+ }+ }++ const inFlight = request();+ documentIngestInFlight.set(key, inFlight);+ const clearRequest = () => {+ if (documentIngestInFlight.get(key) === inFlight) {+ documentIngestInFlight.delete(key);+ }+ };+ void inFlight.then(clearRequest, clearRequest);+ return inFlight;+};diff --git a/web/src/hooks/use-document-request.ts b/web/src/hooks/use-document-request.tsindex fc017ceb00..c7f5032c1f 100644--- a/web/src/hooks/use-document-request.ts+++ b/web/src/hooks/use-document-request.ts@@ -35,7 +35,10 @@ import {} from '@/interfaces/request/document';import i18n from '@/locales/config';import { EMPTY_METADATA_FIELD } from '@/pages/dataset/dataset/use-select-filters';-import { isDocumentProcessing } from '@/pages/dataset/dataset/utils';+import {+ isDocumentProcessing,+ isDocumentStopping,+} from '@/pages/dataset/dataset/utils';import documentStructureService from '@/services/document-structure-service';import { buildDocumentIngestPayload } from '@/services/document-ingest-adapter';import kbService, {@@ -63,6 +66,12 @@ import { get } from 'lodash';import { useCallback, useEffect, useMemo, useState } from 'react';import { IHighlight } from 'react-pdf-highlighter';import { useParams } from 'react-router';+import {+ getCancelRequestInterval,+ markCancelRequested,+ observeStoppingDocuments,+} from './cancel-stop-loss';+import { sendDocumentIngest } from './document-ingest-in-flight';import {useGetPaginationWithRouter,useHandleSearchChange,@@ -85,8 +94,6 @@ export const enum DocumentStructureApiAction {DeleteDocumentStructureGraph = 'deleteDocumentStructureGraph',}-const documentIngestInFlight = new Map<string, Promise<unknown>>();-export const DocumentStructureKeys = {graph: (datasetId: string, documentId: string) =>[@@ -186,23 +193,38 @@ export const useFetchDocumentList = (loop = true) => {const { pagination, setPagination } = useGetPaginationWithRouter();const { id } = useParams();const queryClient = useQueryClient();+ const isGo = useIsGoBackend();const debouncedSearchString = useDebounce(searchString, { wait: 500 });const { filterValue, handleFilterSubmit, checkValue } =useHandleFilterSubmit();- const { data, isFetching: loading } = useQuery<{+ const {+ data,+ isFetching: loading,+ dataUpdatedAt,+ } = useQuery<{docs: IDocumentInfo[];total: number;has_active_tasks?: boolean;}>({queryKey: DocumentKeys.list(debouncedSearchString, pagination, filterValue),initialData: { docs: [], total: 0, has_active_tasks: false },- refetchInterval: (query) =>- loop &&- (query.state.data?.has_active_tasks ||- !!query.state.data?.docs.some(isDocumentProcessing))- ? 5000- : false,+ refetchInterval: (query) => {+ if (!loop) return false;+ const current = query.state.data;+ if (!current) return false;+ const stoppingIds = current.docs+ .filter(isDocumentStopping)+ .map((doc) => doc.id);+ if (stoppingIds.length > 0) {+ return getCancelRequestInterval(stoppingIds);+ }+ if (current.has_active_tasks || current.docs.some(isDocumentProcessing)) {+ return 5000;+ }+ return false;+ },+ refetchIntervalInBackground: true,enabled: !!knowledgeId || !!id,queryFn: async () => {let run = [] as any;@@ -261,6 +283,38 @@ export const useFetchDocumentList = (loop = true) => {});}, [data.docs, queryClient]);+ // Stop-loss: observe the documents on every poll. This starts the window+ // for cancels first seen here, prunes trackers for observed ids that left+ // the stopping state, and re-sends one cancel request for the overdue ones.+ // The effect is keyed on dataUpdatedAt too: structural sharing keeps+ // data.docs reference-identical while a stopped document's fields no longer+ // change, which is exactly the stuck case this has to fire in.+ useEffect(() => {+ if (!isGo) {+ return;+ }+ const overdueIds = observeStoppingDocuments(+ data.docs.map((doc) => doc.id),+ data.docs.filter(isDocumentStopping).map((doc) => doc.id),+ );+ if (overdueIds.length === 0) {+ return;+ }+ void sendDocumentIngest(+ { documentIds: overdueIds, run: 2 },+ () =>+ kbService.documentIngest(+ buildDocumentIngestPayload({ documentIds: overdueIds, run: 2 }),+ ),+ { force: true },+ ).then(+ () => {+ queryClient.invalidateQueries({ queryKey: DocumentKeys.all() });+ },+ () => {},+ );+ }, [data.docs, dataUpdatedAt, isGo, queryClient]);+return {loading,searchString,@@ -440,8 +494,46 @@ export const useRunDocument = () => {),};});+ } else if (run === 2) {+ // Optimistic STOPPING so the row locks immediately; a fast poll then+ // picks up the real status. Stage timestamps in the same pass so the+ // list's stop-loss can downgrade the interval and retry an overdue+ // cancel exactly once.+ // A list refetch already in flight when the click landed would+ // resolve with the pre-cancel row and overwrite the optimistic+ // STOPPING below, so drop it first. This must stay ahead of the+ // optimistic write: cancelQueries reverts a query to the state it had+ // when that refetch started, which would roll the write back. The+ // all() prefix also covers the byIds views, so their in-flight+ // refetches are dropped the same way.+ await queryClient.cancelQueries({ queryKey: DocumentKeys.all() });+ if (isGo) {+ markCancelRequested(documentIds);+ }+ const documentIdSet = new Set(documentIds);+ queryClient.setQueriesData<{+ docs: IDocumentInfo[];+ total: number;+ }>({ queryKey: DocumentKeys.all() }, (current) => {+ if (!current) {+ return current;+ }+ return {+ ...current,+ docs: current.docs.map((doc) =>+ documentIdSet.has(doc.id)+ ? {+ ...doc,+ ...(isGo+ ? { ingestion_status: IngestionTaskStatus.STOPPING }+ : { run: RunningStatus.CANCEL }),+ }+ : doc,+ ),+ };+ });}- if (run !== 1) {+ if (run !== 1 && run !== 2) {queryClient.invalidateQueries({queryKey: DocumentKeys.all(),});@@ -481,27 +573,7 @@ export const useRunDocument = () => {documentIds: string[];run: number;option?: { delete: boolean; apply_kb: boolean };- }) => {- const key = JSON.stringify({- documentIds: [...params.documentIds].sort(),- run: params.run,- option: params.option || null,- });- const existingRequest = documentIngestInFlight.get(key);- if (existingRequest) {- return existingRequest;- }-- const request = mutateAsync(params);- documentIngestInFlight.set(key, request);- const clearRequest = () => {- if (documentIngestInFlight.get(key) === request) {- documentIngestInFlight.delete(key);- }- };- void request.then(clearRequest, clearRequest);- return request;- },+ }) => sendDocumentIngest(params, () => mutateAsync(params)),[mutateAsync],);