Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

50.6 KB
Raw Blame
//
//  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.
//

package service

import (
	"context"
	"errors"
	"fmt"
	"runtime"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	"ragflow/internal/common"
	"ragflow/internal/dao"
	"ragflow/internal/entity"
	"ragflow/internal/ingestion"
	"ragflow/internal/ingestion/knowledge_compile"
	pipelinepkg "ragflow/internal/ingestion/pipeline"
	taskpkg "ragflow/internal/ingestion/task"
	servicepkg "ragflow/internal/service"
	documentpkg "ragflow/internal/service/document"
	"ragflow/internal/utility"

	"github.com/cenkalti/backoff/v5"
	"go.uber.org/zap"
)

// defaultHeartbeatInterval paces the InProgress() working pulse that keeps an
// in-flight message's ack deadline renewed while a worker parses. It MUST stay
// comfortably below the consumer's ack deadline, which the server normalizes
// to BackOff[0] = 5s (see NatsEngine.InitConsumer): a pulse slower than the
// deadline lets the broker redeliver mid-run. A duplicate delivery renews its
// lease but remains unsettled until the owning worker reaches a durable
// outcome. If the worker or process stops, the broker can redeliver the
// unsettled message after the ack deadline.
const defaultHeartbeatInterval = 2 * time.Second

type Ingestor struct {
	id     string
	name   string
	ctx    context.Context // execution context for handles already owned by workers
	cancel context.CancelFunc

	dispatchCtx    context.Context // worker queue registration and Pull lifecycle
	dispatchCancel context.CancelFunc

	// Configuration
	maxConcurrency           int32
	supportedDocTypes        []string
	version                  string
	heartbeatInterval        time.Duration
	memoryReconcileInterval  time.Duration
	memoryReconcileBatchSize int

	// Runtime state
	currentTasks         map[string]struct{} // set of task IDs currently claimed by a worker
	tasksMu              sync.RWMutex
	pendingCompileEvents map[string]pendingDocumentCompileEvent
	compileEventsMu      sync.Mutex
	activeWorkers        atomic.Int32 // number of worker goroutines currently in workerLoop
	activeLeases         map[*Heartbeat]*activeLease
	leasesMu             sync.Mutex
	stopLeases           atomic.Bool

	// Shutdown channel - receive on this to trigger graceful shutdown
	ShutdownCh chan struct{}

	// Worker pool
	workerQueue  chan *worker
	dispatcherWg sync.WaitGroup
	workerWg     sync.WaitGroup
	startOnce    sync.Once
	startErr     error
	workerOnce   sync.Once // guards startWorkerPool; must NOT be startOnce (Start wraps start() in startOnce, and start() calls startWorkerPool -> re-entry deadlock)
	stopOnce     sync.Once // guards close(ShutdownCh) against double-close on repeated Stop

	ingestionTaskSvc *servicepkg.IngestionTaskService
	docState         *docStateUpdater
	// memorySvc runs async memory-extraction tasks (TaskKindMemory) that share
	// the worker pool with ingestion tasks. nil disables memory extraction
	// (e.g. tests that don't exercise it).
	memorySvc *servicepkg.MemoryMessageService

	// knowledgeCompile is the dataset-level post-processing consumer (§11,
	// Option E) owned by this ingestor. It is driven by kcConcurrency owned
	// worker goroutines, each running the Consumer's Run loop (poll MySQL
	// scheduling rows + NATS notify → claim closed batch → merge → ack). The
	// MySQL knowledge_compile_docs and its queue tables are the scheduling
	// system of record and the source of same-KB serialization, so different
	// datasets compile in parallel while the same dataset is serialized by its
	// claim row. The workers are started/joined within the ingestor (via
	// compileWg), so they share its lifecycle and goroutine set instead of
	// running as a separate service goroutine.
	knowledgeCompile *knowledge_compile.Consumer
	kcLLMID          string
	kcEmbedding      string
	kcConcurrency    int32 // number of parallel dataset-level compile workers

	compileWg         sync.WaitGroup
	memoryReconcileWg sync.WaitGroup

	// runDocumentTask dispatches to the migrated task handler path.
	// Tests may override this to verify branch routing without invoking
	// the full downstream stack.
	runDocumentTask func(ctx context.Context, ingestionTask *entity.IngestionTask) error

	// runMemoryTask dispatches one async memory-extraction task. Tests may
	// override this to inject a panicking/failing runner without a live DB or
	// real MemoryMessageService. Defaults to defaultRunMemoryTask, which calls
	// memorySvc.HandleSaveToMemoryTask with the envelope task id and a unique
	// database lease owner.
	runMemoryTask func(ctx context.Context, taskID, leaseOwner string) (servicepkg.MemoryTaskDisposition, error)

	// reconcileMemoryTasks republishes due durable memory task wake-ups. Tests
	// may replace it to verify lifecycle behavior without a database or broker.
	reconcileMemoryTasks func(ctx context.Context) error

	// 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

	// deps bundles the process-wide singletons the ingestor depends on
	// (document engine, relational handle, task broker, kvrocks). It is
	// injected at construction (NewIngestor) rather than read from the
	// package globals (arch_test.go guards that rule). A nil bundle is
	// normalized to an empty Deps; a nil sub-value disables the matching
	// capability (e.g. a nil DB skips startup task scheduling).
	deps *ingestion.Deps
}

type worker struct {
	id    int32
	inbox chan common.TaskHandle
}

type activeLease struct {
	abandoned atomic.Bool
}

type pendingDocumentCompileEvent struct {
	tenantID  string
	variants  []string
	taskTypes []string
}

// NewIngestor constructs the ingestion consumer. db, mq, and kv are the
// process-wide relational handle, message-queue engine, and kvrocks client;
// they are resolved once at the server assembly point and injected here rather
// than read from the package globals (arch_test.go guards that rule).
func NewIngestor(name string, maxConcurrency int32, supportedTypes []string, deps *ingestion.Deps) *Ingestor {
	if maxConcurrency <= 0 {
		maxConcurrency = int32(runtime.NumCPU())
	}
	if deps == nil {
		deps = &ingestion.Deps{}
	}
	ctx, cancel := context.WithCancel(context.Background())
	dispatchCtx, dispatchCancel := context.WithCancel(context.Background())
	id := common.GenerateUUID()
	ingestor := &Ingestor{
		id:                       id,
		name:                     name,
		ctx:                      ctx,
		cancel:                   cancel,
		dispatchCtx:              dispatchCtx,
		dispatchCancel:           dispatchCancel,
		maxConcurrency:           maxConcurrency,
		supportedDocTypes:        supportedTypes,
		version:                  "1.0.0",
		currentTasks:             make(map[string]struct{}),
		pendingCompileEvents:     make(map[string]pendingDocumentCompileEvent),
		activeLeases:             make(map[*Heartbeat]*activeLease),
		workerQueue:              make(chan *worker, maxConcurrency),
		ShutdownCh:               make(chan struct{}, 1),
		ingestionTaskSvc:         servicepkg.NewIngestionTaskService(),
		docState:                 newDocStateUpdater(),
		heartbeatInterval:        defaultHeartbeatInterval,
		memoryReconcileInterval:  defaultMemoryTaskReconcileInterval,
		memoryReconcileBatchSize: defaultMemoryTaskReconcileBatchSize,
		deps:                     deps,
	}
	ingestor.runDocumentTask = ingestor.defaultRunDocumentTask
	ingestor.runMemoryTask = ingestor.defaultRunMemoryTask
	ingestor.reconcileMemoryTasks = ingestor.defaultReconcileMemoryTasks
	ingestor.cancelCheck = ingestor.defaultCancelCheck
	ingestor.kcConcurrency = maxConcurrency // parallel dataset-level compile workers default to the task width
	return ingestor
}

func (e *Ingestor) ID() string {
	return e.id
}

// consumeErrorBackoff paces failed Pull requests so a persistent MQ failure
// does not pin a CPU. The backoff is cancellable and does not block another
// idle worker from making its own Pull request.
const consumeErrorBackoff = 1 * time.Second

const taskPullRequestTimeout = 1 * time.Second

const (
	defaultMemoryTaskReconcileInterval  = 10 * time.Second
	defaultMemoryTaskReconcileBatchSize = 100
)

func (e *Ingestor) Start() error {
	common.Info(fmt.Sprintf("Ingestor %s initialized", e.id))
	e.startOnce.Do(func() {
		e.startErr = e.start()
	})
	return e.startErr
}

// start runs the full startup sequence. It is invoked at most once (guarded by
// startOnce in Start) so repeated Start calls cannot launch duplicate worker
// pools, compile consumers, or consume loops, and the first initialization
// error is retained and returned to every later caller.
func (e *Ingestor) start() error {
	msgQueueEngine := e.deps.MQ
	if msgQueueEngine == nil {
		return fmt.Errorf("message queue engine not initialized; run engine.InitMessageQueue first")
	}
	if err := msgQueueEngine.InitConsumer(common.TaskSubject); err != nil {
		return err
	}
	if e.deps.DB != nil {
		if err := e.ingestionTaskSvc.ScheduleCreatedTasks(e.ctx); err != nil {
			common.Warn(fmt.Sprintf("schedule CREATED ingestion tasks at startup: %v", err))
		}
	}

	// Start the task workers, memory reconciler, and dataset compile consumer as
	// owned goroutines joined by Stop. Start follows
	// the standard lifecycle contract: it returns immediately after kicking
	// these off rather than blocking on the consume loop itself.
	e.startWorkerPool()
	e.startMemoryTaskReconciler()
	e.startDatasetKnowledgeCompile()

	// Run the main tasks.RAGFLOW dispatcher off the caller's goroutine so Start
	// returns promptly. Stop joins it before cancelling task execution.
	e.dispatcherWg.Add(1)
	go e.consumeLoop()

	return nil
}

// startMemoryTaskReconciler starts the owned loop that recovers durable memory
// tasks whose initial wake-up was lost or whose retry became due.
func (e *Ingestor) startMemoryTaskReconciler() {
	if e.memorySvc == nil || e.deps.DB == nil || e.reconcileMemoryTasks == nil {
		return
	}
	e.memoryReconcileWg.Add(1)
	go e.memoryTaskReconcileLoop()
}

// memoryTaskReconcileLoop performs one startup pass and then polls until the
// ingestor execution context is canceled.
func (e *Ingestor) memoryTaskReconcileLoop() {
	defer e.memoryReconcileWg.Done()
	reconcile := func() {
		if err := e.reconcileMemoryTasks(e.ctx); err != nil && !errors.Is(err, context.Canceled) {
			common.Warn(fmt.Sprintf("reconcile durable memory tasks: %v", err))
		}
	}

	reconcile()
	interval := e.memoryReconcileInterval
	if interval <= 0 {
		interval = defaultMemoryTaskReconcileInterval
	}
	ticker := time.NewTicker(interval)
	defer ticker.Stop()
	for {
		select {
		case <-e.ctx.Done():
			return
		case <-ticker.C:
			reconcile()
		}
	}
}

// defaultReconcileMemoryTasks publishes one bounded batch of due wake-ups.
func (e *Ingestor) defaultReconcileMemoryTasks(ctx context.Context) error {
	batchSize := e.memoryReconcileBatchSize
	if batchSize <= 0 {
		batchSize = defaultMemoryTaskReconcileBatchSize
	}
	return e.memorySvc.ReconcileMemoryTasks(ctx, batchSize)
}

// consumeLoop is the worker dispatcher for tasks.RAGFLOW. It waits for an
// available worker before synchronously pulling one task, preventing prefetch
// and limiting this ingestor to one outstanding broker request.
func (e *Ingestor) consumeLoop() {
	defer e.dispatcherWg.Done()
	msgQueueEngine := e.deps.MQ
	for {
		var w *worker
		select {
		case <-e.dispatchCtx.Done():
			return
		case w = <-e.workerQueue:
		}
		if e.dispatchCtx.Err() != nil {
			e.returnWorker(w)
			return
		}

		pullStart := time.Now()
		pullCtx, cancel := context.WithTimeout(e.dispatchCtx, taskPullRequestTimeout)
		handle, err := msgQueueEngine.PullMessage(pullCtx)
		cancel()
		if err != nil {
			e.returnWorker(w)
			e.logPullError(err)
			if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
				e.waitAfterPullError()
			}
			continue
		}
		if handle == nil {
			e.returnWorker(w)
			continue
		}

		recvTime := time.Now()
		latency := time.Since(pullStart)
		common.Debug(fmt.Sprintf("Pull delivered handle in %v", latency),
			zap.Duration("pull_first_handle_latency", latency),
		)
		select {
		case w.inbox <- handle:
			handoffDuration := time.Since(recvTime)
			common.Debug(fmt.Sprintf("Handed off handle to worker %d in %v", w.id, handoffDuration),
				zap.Int32("worker_id", w.id),
				zap.Duration("handoff_duration", handoffDuration),
			)
		case <-e.dispatchCtx.Done():
			if err = handle.Nack(); err != nil {
				common.Error("nack task after shutdown interrupted handoff", err)
			}
			return
		}
	}
}

func (e *Ingestor) logPullError(err error) {
	if err == nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
		return
	}
	common.Error("error pulling task message", err)
}

func (e *Ingestor) waitAfterPullError() {
	select {
	case <-time.After(consumeErrorBackoff):
	case <-e.dispatchCtx.Done():
	}
}

func (e *Ingestor) returnWorker(w *worker) {
	select {
	case e.workerQueue <- w:
	case <-e.dispatchCtx.Done():
	}
}

// SetMemoryMessageService installs the memory-extraction service used by
// TaskKindMemory tasks that share the worker pool. Call it before Start; a nil
// value disables memory extraction (received memory tasks are ack-skipped).
func (e *Ingestor) SetMemoryMessageService(memorySvc *servicepkg.MemoryMessageService) {
	e.memorySvc = memorySvc
}

// SetKnowledgeCompileModelConfig supplies the default LLM/embedding model ids
// used by the dataset-level compile consumer's deduper. Call it before Start.
func (e *Ingestor) SetKnowledgeCompileModelConfig(llmID, embedding string) {
	e.kcLLMID = llmID
	e.kcEmbedding = embedding
}

// SetKnowledgeCompileConcurrency sets the number of parallel dataset-level
// compile workers. Multiple datasets compile concurrently (each worker runs its
// own Run loop that claims a KB's closed batch and merges it); the per-KB claim
// row serializes the same dataset, preserving the §11 O(unique pairs)
// merged-dedup invariant. Call before Start. A value <= 0 falls back to
// runtime.NumCPU() at start time.
func (e *Ingestor) SetKnowledgeCompileConcurrency(n int32) {
	e.kcConcurrency = n
}

// startDatasetKnowledgeCompile provisions the dataset-level compile scheduling
// store (MySQL scheduling tables + NATS notify subject), builds the
// Consumer, and starts the owned compile-worker pool. The consumer is driven by
// the ingestor's own goroutine set — kcConcurrency compileLoop workers, each
// running the Consumer's Run loop (see §11.7 of knowledge_compile_design.md) —
// so its lifecycle shares the ingestor's. Best-effort: any provisioning failure
// is logged and the pipeline continues to write available_int=0 compiled
// chunks; they just won't be merged until a scheduler is available.
func (e *Ingestor) startDatasetKnowledgeCompile() {
	mq := e.deps.MQ
	if mq == nil {
		common.Warn("message queue not initialized; dataset-level compile consumer disabled")
		return
	}
	knowledge_compile.SetModelConfig(e.kcLLMID, e.kcEmbedding)
	if err := knowledge_compile.Provision(e.ctx, mq, e.deps.DB); err != nil {
		common.Warn(fmt.Sprintf("dataset-level compile consumer unavailable; compiled chunks will not be merged: %v", err))
		return
	}
	e.knowledgeCompile = e.newKnowledgeCompileConsumer()
	n := e.kcConcurrency
	if n <= 0 {
		n = int32(runtime.NumCPU())
	}
	// kcConcurrency owned workers, each running its own Run loop. Concurrency is
	// bounded by the goroutine count itself (no extra semaphore). Different
	// datasets compile in parallel while the same dataset is serialized by its
	// MySQL claim row — preserving the §11 invariant that merged dedup stays
	// O(unique pairs) instead of O(N).
	for i := int32(0); i < n; i++ {
		e.compileWg.Add(1)
		go e.compileLoop(i)
	}
}

// newKnowledgeCompileConsumer builds the dataset-level knowledge-compile
// consumer with its storage dependencies injected from the ingestor's Deps
// bundle. The consumer fails fast on storage access when WithStorage is omitted
// (see knowledge_compile.NewConsumer), so the doc engine + db MUST be threaded
// through here — never construct it with a bare NewConsumer in production. The
// assembly point can assert c.IsStorageConfigured() before relying on merges.
func (e *Ingestor) newKnowledgeCompileConsumer() *knowledge_compile.Consumer {
	return knowledge_compile.NewConsumer(
		knowledge_compile.DefaultClaimer(),
		knowledge_compile.WithStorage(e.deps.Engine, e.deps.DB),
	)
}

// compileLoop is one of the owned dataset-level compile workers. It runs the
// Consumer's Run loop (poll scheduling rows + NATS notify → claim closed batch
// → merge → ack). kcConcurrency such goroutines run in parallel, so different
// datasets compile concurrently while the same dataset is serialized by its
// claim row. All are joined in Stop via compileWg.
func (e *Ingestor) compileLoop(id int32) {
	defer e.compileWg.Done()
	if e.knowledgeCompile != nil {
		e.knowledgeCompile.Run(e.ctx)
	}
}

// handleAndExecute owns one handle from its hand-off to final settlement. It
// runs only in the worker that received the handle through its private inbox.
func (e *Ingestor) handleAndExecute(handle common.TaskHandle) {
	startTime := time.Now()
	taskMessage := handle.GetMessage()
	common.Info(fmt.Sprintf("Received task id: %s, type: %s", taskMessage.TaskID, taskMessage.TaskType),
		zap.String("task_id", taskMessage.TaskID),
		zap.String("task_type", taskMessage.TaskType),
	)
	defer func() {
		settlementDuration := time.Since(startTime)
		common.Debug(fmt.Sprintf("Task %s settlement finished in %v", taskMessage.TaskID, settlementDuration),
			zap.String("task_id", taskMessage.TaskID),
			zap.Duration("settlement_duration", settlementDuration),
		)
	}()

	hb := NewHeartbeat(taskMessage.TaskID, handle, e.heartbeatInterval).
		WithContext(context.Background())
	hb.Start()
	e.registerLease(hb)
	defer e.unregisterLease(hb)

	// Memory-extraction tasks share the tasks.RAGFLOW consumer and the worker
	// pool with ingestion tasks. They do NOT use the ingestion state machine.
	if taskMessage.TaskType == common.TaskTypeMemory {
		if e.memorySvc == nil {
			common.Warn(fmt.Sprintf("memory task %s received but memory extractor is disabled, ack", taskMessage.TaskID))
			e.ackHandle(hb, handle, taskMessage.TaskID)
			return
		}
		if taskMessage.TaskID == "" {
			common.Warn("memory task with empty task id received, ack")
			e.ackHandle(hb, handle, taskMessage.TaskID)
			return
		}
		taskCtx := taskpkg.NewMemoryTaskContextForScheduling(e.ctx, taskMessage.TaskID, handle)
		e.executeMemoryTaskWithHeartbeat(e.ctx, taskCtx, hb)
		return
	}

	if taskMessage.TaskType != common.TaskTypeIngestionTask {
		common.Info(fmt.Sprintf("task %s is not an ingestion task", taskMessage.TaskID))
		e.ackHandle(hb, handle, taskMessage.TaskID)
		return
	}

	task, err := e.ingestionTaskSvc.TransitionTaskToRunning(e.ctx, taskMessage.TaskID)
	if err != nil {
		if errors.Is(err, common.ErrTaskNotFound) {
			common.Warn(fmt.Sprintf("task %s not found, skipping", taskMessage.TaskID))
			e.ackHandle(hb, handle, taskMessage.TaskID)
			return
		}
		common.Error(fmt.Sprintf("error setting task %s to running", taskMessage.TaskID), err)
		e.nackHandle(hb, handle, taskMessage.TaskID)
		return
	}
	if task == nil {
		common.Info(fmt.Sprintf("task %s is already removed", taskMessage.TaskID))
		e.ackHandle(hb, handle, taskMessage.TaskID)
		return
	}

	switch task.Status {
	case common.COMPLETED, common.STOPPED, common.FAILED:
		common.Info(fmt.Sprintf("task %s is already %s", taskMessage.TaskID, task.Status))
		e.ackHandle(hb, handle, taskMessage.TaskID)
		return
	case common.RUNNING:
		validatedTask, validateErr := e.ingestionTaskSvc.ReloadAndValidateRunIdentity(e.ctx, task.ID)
		if validateErr != nil {
			var identityErr *servicepkg.InvalidRunIdentityError
			if errors.As(validateErr, &identityErr) {
				common.Error(fmt.Sprintf("task %s has permanent run identity error: %s", task.ID, identityErr.Reason), validateErr)
				if e.markFailed(e.ctx, task.ID, fmt.Sprintf("Task rejected: invalid run identity (%s).", identityErr.Reason)) {
					e.recordTerminalPipelineLog(e.ctx, task, string(entity.TaskStatusFail), fmt.Sprintf("Task rejected: invalid run identity (%s).", identityErr.Reason))
					e.ackHandle(hb, handle, taskMessage.TaskID)
				} else {
					e.nackHandle(hb, handle, taskMessage.TaskID)
				}
				return
			}
			common.Error(fmt.Sprintf("validate run identity for task %s", task.ID), validateErr)
			e.nackHandle(hb, handle, taskMessage.TaskID)
			return
		}
		task = validatedTask
		if !e.claimTask(task.ID) {
			common.Warn(fmt.Sprintf("task %s redelivered while worker still processing, renew lease (task_id=%s doc_id=%s kb_id=%s)",
				taskMessage.TaskID, task.ID, task.DocumentID, task.DatasetID))
			e.renewDuplicateHandle(hb, handle, taskMessage.TaskID)
			return
		}
		// Claim the task before applying document/log side effects. A duplicate
		// delivery must only renew its broker lease; resetting the document here
		// would race the owning worker and erase its in-flight progress.
		e.ingestionTaskSvc.PrepareValidatedRun(e.ctx, task)
	default:
		common.Warn(fmt.Sprintf("task %s in unexpected status %s, ack-skip", taskMessage.TaskID, task.Status))
		e.ackHandle(hb, handle, taskMessage.TaskID)
		return
	}

	taskCtx := taskpkg.NewTaskContextForScheduling(e.ctx, task)
	taskCtx.Handle = handle
	e.executeTaskWithHeartbeat(e.ctx, taskCtx, hb)
}

func (e *Ingestor) ackHandle(hb *Heartbeat, handle common.TaskHandle, taskID string) {
	hb.Stop()
	if e.leaseAbandoned(hb) {
		return
	}
	if err := handle.Ack(); err != nil {
		common.Error(fmt.Sprintf("ack task %s", taskID), err)
	}
}

func (e *Ingestor) nackHandle(hb *Heartbeat, handle common.TaskHandle, taskID string) {
	hb.Stop()
	if e.leaseAbandoned(hb) {
		return
	}
	if err := handle.Nack(); err != nil {
		common.Error(fmt.Sprintf("nack task %s", taskID), err)
	}
}

func (e *Ingestor) renewDuplicateHandle(hb *Heartbeat, handle common.TaskHandle, taskID string) {
	hb.Stop()
	if e.leaseAbandoned(hb) {
		return
	}
	if err := handle.InProgress(); err != nil {
		common.Error(fmt.Sprintf("renew redelivered task %s", taskID), err)
	}
}

func (e *Ingestor) registerLease(hb *Heartbeat) {
	lease := &activeLease{}
	e.leasesMu.Lock()
	stop := e.stopLeases.Load()
	if stop {
		lease.abandoned.Store(true)
	}
	e.activeLeases[hb] = lease
	e.leasesMu.Unlock()
	if stop {
		hb.Stop()
	}
}

func (e *Ingestor) unregisterLease(hb *Heartbeat) {
	e.leasesMu.Lock()
	delete(e.activeLeases, hb)
	e.leasesMu.Unlock()
}

func (e *Ingestor) leaseAbandoned(hb *Heartbeat) bool {
	e.leasesMu.Lock()
	lease := e.activeLeases[hb]
	e.leasesMu.Unlock()
	return lease != nil && lease.abandoned.Load()
}

func (e *Ingestor) stopActiveLeases() {
	e.leasesMu.Lock()
	e.stopLeases.Store(true)
	leases := make([]*Heartbeat, 0, len(e.activeLeases))
	for heartbeat, lease := range e.activeLeases {
		lease.abandoned.Store(true)
		leases = append(leases, heartbeat)
	}
	e.leasesMu.Unlock()

	for _, lease := range leases {
		lease.Stop()
	}
}

func (e *Ingestor) startWorkerPool() {
	e.workerOnce.Do(func() {
		for i := int32(0); i < e.maxConcurrency; i++ {
			e.workerWg.Add(1)
			w := &worker{id: i, inbox: make(chan common.TaskHandle)}
			go e.workerLoop(w)
		}
		common.Info(fmt.Sprintf("Worker pool started with %d workers", e.maxConcurrency))
	})
}

func (e *Ingestor) workerLoop(w *worker) {
	defer e.workerWg.Done()
	defer e.activeWorkers.Add(-1)
	e.activeWorkers.Add(1)
	common.Info(fmt.Sprintf("Worker %d started", w.id))
	for {
		select {
		case e.workerQueue <- w:
		case <-e.dispatchCtx.Done():
			return
		}

		select {
		case handle := <-w.inbox:
			e.handleAndExecute(handle)
		case <-e.dispatchCtx.Done():
			return
		}
	}
}

func (e *Ingestor) executeMemoryTask(ctx context.Context, taskCtx *taskpkg.TaskContext) {
	hb := NewHeartbeat(taskCtx.ID(), taskCtx.Handle, e.heartbeatInterval).WithContext(ctx)
	hb.Start()
	e.executeMemoryTaskWithHeartbeat(ctx, taskCtx, hb)
}

// executeMemoryTaskWithHeartbeat settles a memory handle with the heartbeat
// that the receiving worker started before admission.
func (e *Ingestor) executeMemoryTaskWithHeartbeat(ctx context.Context, taskCtx *taskpkg.TaskContext, hb *Heartbeat) {
	taskID := taskCtx.ID()

	settleAck := false
	defer func() {
		hb.Stop()
		if r := recover(); r != nil {
			common.Error(fmt.Sprintf("memory task %s panicked: %v", taskID, r), fmt.Errorf("%v", r))
		}
		if taskCtx.Handle == nil || e.leaseAbandoned(hb) {
			return
		}
		if settleAck {
			if err := taskCtx.Handle.Ack(); err != nil {
				common.Error(fmt.Sprintf("ack memory task %s", taskID), err)
			}
		}
	}()

	common.Info(fmt.Sprintf("Starting memory task %s", taskID))
	if taskCtx.Handle == nil {
		common.Warn("memory task handle is nil, skip")
		return
	}
	if e.memorySvc == nil {
		common.Warn(fmt.Sprintf("memory task %s: memory extractor disabled, ack", taskID))
		settleAck = true
		return
	}
	leaseOwner := fmt.Sprintf("%s:%s", e.id, common.GenerateUUID())
	disposition, err := e.runMemoryTask(ctx, taskID, leaseOwner)
	if err != nil {
		common.Error(fmt.Sprintf("memory task %s execution failed", taskID), err)
	}
	if disposition == servicepkg.MemoryTaskAcknowledge {
		settleAck = true
		common.Info(fmt.Sprintf("Memory task %s delivery acknowledged", taskID))
		return
	}
	common.Warn(fmt.Sprintf("memory task %s delivery left unsettled for durable recovery", taskID))
}

// defaultRunMemoryTask is the production memory-task runner. It is held behind
// the runMemoryTask field so tests can substitute a panicking/failing runner
// without a live DB or real MemoryMessageService.
func (e *Ingestor) defaultRunMemoryTask(ctx context.Context, taskID, leaseOwner string) (servicepkg.MemoryTaskDisposition, error) {
	return e.memorySvc.HandleSaveToMemoryTask(ctx, taskID, leaseOwner)
}

func (e *Ingestor) executeTask(ctx context.Context, taskCtx *taskpkg.TaskContext) {
	hb := NewHeartbeat(taskCtx.ID(), taskCtx.Handle, e.heartbeatInterval).WithContext(taskCtx.Ctx)
	hb.Start()
	e.executeTaskWithHeartbeat(ctx, taskCtx, hb)
}

// executeTaskWithHeartbeat executes one document task using the lease already
// started by the worker that owns its Handle.
func (e *Ingestor) executeTaskWithHeartbeat(ctx context.Context, taskCtx *taskpkg.TaskContext, hb *Heartbeat) {
	task := taskCtx.IngestionTask
	common.Info(fmt.Sprintf("Starting task %s", task.ID))

	// Release the claim when executeTask returns so that a future
	// redelivery (after restart) can re-claim the task.
	defer e.releaseTask(task.ID)

	// Derive a per-task cancelable context so that an external cancel
	// signal (Redis cancel flag, mirrored from Python's {task_id}-cancel)
	// can stop only this task without affecting the whole Ingestor.
	perTaskCtx, perTaskCancel := context.WithCancel(taskCtx.Ctx)
	taskCtx.Ctx = perTaskCtx
	cancelDone := make(chan struct{})
	pollExited := make(chan struct{})
	go func() {
		defer close(pollExited)
		e.pollCancel(task.ID, perTaskCancel, cancelDone)
	}()
	defer func() {
		close(cancelDone)
		<-pollExited
		perTaskCancel()
	}()

	// Synchronous check: if already cancelled (e.g. flag set between MQ
	// delivery and worker claim), stop before the pipeline even starts.
	if e.cancelCheck(ctx, task.ID) {
		common.Info(fmt.Sprintf("Task %s cancel flag detected before pipeline start, cancelling", task.ID))
		perTaskCancel()
	}

	e.settleMessage(ctx, taskCtx, hb, func(ctx context.Context) bool {
		return e.runTask(ctx, task)
	})
}

// markStopped transitions the task to STOPPED (terminal). It first calls
// RequestStop to handle RUNNING → STOPPING, then MarkStopped for the final
// STOPPING → STOPPED transition. Finally it cleans up the Redis cancel flag
// so that a future retry of the same task does not immediately re-cancel.
// The caller's context is detached before touching the DB: markStopped runs
// on failure/cancel/shutdown paths whose context is typically already
// cancelled, and a contaminated context would make the terminal write fail
// exactly when it matters most.
func (e *Ingestor) markStopped(ctx context.Context, taskID string) bool {
	ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
	defer cancel()
	if _, err := e.ingestionTaskSvc.RequestStop(ctx, taskID); err != nil {
		common.Error(fmt.Sprintf("markStopped: RequestStop task %s: %v", taskID, err), err)
		return false
	}
	if err := e.ingestionTaskSvc.MarkStopped(ctx, taskID); err != nil {
		common.Error(fmt.Sprintf("markStopped: MarkStopped task %s: %v", taskID, err), err)
		return false
	}
	if rc := e.deps.KV; rc != nil {
		utility.BestEffort(fmt.Sprintf("clear cancel flag for %s", taskID), func() error {
			rc.Delete(ctx, fmt.Sprintf("%s-cancel", taskID))
			return nil // Delete returns bool; the bool does not distinguish "not found" from "error"
		})
	}
	return true
}

// markFailed persists FAILED status for the task and reports whether the
// terminal status was durably written, so the caller can decide Ack vs Nack.
// The caller's context is detached before touching the DB (see markStopped):
// failure paths usually carry an already-cancelled context, and losing the
// FAILED write would turn a settled task into a redelivery loop.
func (e *Ingestor) markFailed(ctx context.Context, taskID, reason string) bool {
	ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
	defer cancel()
	if uErr := e.ingestionTaskSvc.MarkFailed(ctx, taskID, reason); uErr != nil {
		common.Error(fmt.Sprintf("Failed to set task %s to FAILED", taskID), uErr)
		return false
	}
	return true
}

// runTask executes the task's business logic — document pipeline and
// completion — behind the heartbeat. It returns whether the task reached a
// durably-persisted terminal status.
func (e *Ingestor) runTask(ctx context.Context, task *entity.IngestionTask) bool {
	select {
	case <-ctx.Done():
		if e.ctx.Err() != nil || e.dispatchCtx.Err() != nil {
			common.Info(fmt.Sprintf("Task %s aborted due to ingestor shutdown, leaving for redelivery", task.ID))
			return false
		}
		common.Info(fmt.Sprintf("Task %s cancelled", task.ID))
		e.markTerminalProgress(task)
		stopped := e.markStopped(context.Background(), task.ID)
		if stopped {
			e.recordTerminalPipelineLog(context.Background(), task, string(entity.TaskStatusCancel), "Task stopped by user.")
		}
		return stopped
	default:
	}

	// Any Redis cancel flag that exists now is stale — a leftover from a previous
	// run whose
	// markStopped failed to delete it. The current run's cancel is signalled
	// by the DB status (STOPPING), which defaultCancelCheck falls back to
	// when the Redis flag is absent. Clearing a stale flag here is safe:
	// a genuine concurrent cancel sets the task to STOPPING in DB.
	if rc := e.deps.KV; rc != nil {
		key := fmt.Sprintf("%s-cancel", task.ID)
		utility.BestEffort(fmt.Sprintf("clear stale cancel flag for %s", task.ID), func() error {
			rc.Delete(ctx, key)
			return nil // Delete returns bool; false may mean "key not found" or "error"
		})
	}

	if err := e.runDocumentTask(ctx, task); err != nil {
		e.clearPendingCompileEvent(task.ID)
		if errors.Is(err, context.Canceled) {
			if e.ctx.Err() != nil || e.dispatchCtx.Err() != nil {
				common.Info(fmt.Sprintf("Task %s pipeline interrupted by ingestor shutdown, leaving for redelivery", task.ID))
				return false
			}
			common.Info(fmt.Sprintf("Task %s cancelled during pipeline", task.ID))
			e.markTerminalProgress(task)
			stopped := e.markStopped(ctx, task.ID)
			if stopped {
				e.recordTerminalPipelineLog(ctx, task, string(entity.TaskStatusCancel), "Task stopped by user.")
			}
			return stopped
		}
		if errors.Is(err, context.DeadlineExceeded) {
			common.Info(fmt.Sprintf("Task %s timed out during pipeline", task.ID))
			e.markTerminalProgress(task)
			ok := e.markFailed(ctx, task.ID, "Task timed out.")
			if ok {
				e.recordTerminalPipelineLog(ctx, task, string(entity.TaskStatusFail), "Task timed out.")
			}
			return ok
		}
		common.Error(fmt.Sprintf("Task %s failed", task.ID), err)
		ok := e.markFailed(ctx, task.ID, fmt.Sprintf("Task failed: %v", err))
		if ok {
			e.recordTerminalPipelineLog(ctx, task, string(entity.TaskStatusFail), fmt.Sprintf("Task failed: %v", err))
		}
		return ok
	}

	if err := e.completeTask(ctx, task.ID); err != nil {
		common.Error(fmt.Sprintf("Task %s update status failed", task.ID), err)
		return false
	}
	e.recordTerminalPipelineLog(ctx, task, string(entity.TaskStatusDone), "Task completed.")
	e.publishPendingCompileEvent(ctx, task)

	common.Info(fmt.Sprintf("Task %s completed", task.ID))
	return true
}

// markTerminalProgress preserves the numeric document state used by existing
// status projections without rewriting the legacy progress_msg column. The
// human-readable explanation is written as a run-scoped terminal event.
func (e *Ingestor) markTerminalProgress(task *entity.IngestionTask) {
	if task == nil || task.DocumentID == "" {
		return
	}
	ctx, cancel := context.WithTimeout(context.WithoutCancel(e.ctx), 5*time.Second)
	defer cancel()
	if err := documentpkg.NewDocumentService().UpdateRunState(ctx, task.DocumentID, -1); err != nil {
		common.Warn(fmt.Sprintf("mark terminal progress for document %s: %v", task.DocumentID, err))
	}
}

// completeTask persists the task's terminal status after a successful pipeline.
// MarkCompleted is retried with backoff for transient (DB) failures only. A
// terminal transition failure - the task is no longer RUNNING because a
// concurrent stop (or another worker) moved it - is NOT retried: the pipeline
// already did the work, so completeOrSettle settles the task to its actual
// terminal state and the caller Acks instead of redelivering.
func (e *Ingestor) completeTask(ctx context.Context, taskID string) error {
	_, err := backoff.Retry(ctx, func() (struct{}, error) {
		return struct{}{}, e.completeOrSettle(ctx, taskID)
	}, backoff.WithMaxTries(3))
	return err
}

// completeOrSettle marks the task COMPLETED, or - if the transition is
// terminally invalid because the task is no longer RUNNING - settles it to its
// actual terminal state. Returns nil once the task is in any terminal state;
// returns a non-terminal (transient) error only for retry-worthy DB failures.
func (e *Ingestor) completeOrSettle(ctx context.Context, taskID string) error {
	if err := e.ingestionTaskSvc.MarkCompleted(ctx, taskID); err != nil {
		if isTerminalTransitionError(err) {
			return e.settleToTerminal(ctx, taskID)
		}
		return err
	}
	return nil
}

// isTerminalTransitionError reports whether err is a state-machine transition
// failure - an invalid transition or a lost optimistic CAS - meaning the task's
// status moved on and MarkCompleted will never succeed as-is. Not retry-worthy;
// the caller settles by the task's current status.
func isTerminalTransitionError(err error) bool {
	var ite *servicepkg.InvalidTaskTransitionError
	var tce *servicepkg.TaskStatusConflictError
	return errors.As(err, &ite) || errors.As(err, &tce)
}

// settleToTerminal finalizes a task whose MarkCompleted failed because it was
// no longer RUNNING. STOPPING is moved to STOPPED via markStopped (which also
// clears the Redis cancel flag so a future retry does not immediately
// re-cancel); already-terminal states (COMPLETED/STOPPED/FAILED) need no
// action. An unexpected status returns an error so the caller nacks and
// redelivery settles it.
func (e *Ingestor) settleToTerminal(ctx context.Context, taskID string) error {
	task, err := e.ingestionTaskSvc.GetTask(ctx, taskID)
	if err != nil {
		return err
	}
	switch task.Status {
	case common.STOPPING:
		if !e.markStopped(ctx, taskID) {
			return fmt.Errorf("task %s: settle to STOPPED failed", taskID)
		}
		return nil
	case common.COMPLETED, common.STOPPED, common.FAILED:
		return nil
	default:
		return fmt.Errorf("task %s in unexpected status %s after transition failure", taskID, task.Status)
	}
}

// settleMessage runs body under an injected heartbeat lease, then settles the MQ message.
// The heartbeat is stopped (and waited on) before ack/nack — see Heartbeat.Stop.
// A panic in body is recovered: the task is marked FAILED and the message is
// Nacked for redelivery, so a single task's panic never crashes the worker.
// Settlement queries the DB for the task's actual status: a terminal state
// (COMPLETED/STOPPED/FAILED) means Ack; anything else means Nack. The body's
// return value is advisory only — DB truth is authoritative (BP1).
func (e *Ingestor) settleMessage(ctx context.Context, taskCtx *taskpkg.TaskContext, hb *Heartbeat, body func(context.Context) bool) (terminal bool) {
	defer func() {
		hb.Stop() // stop heartbeat (and wait) before ack/nack
		if r := recover(); r != nil {
			// Recover the panic so the worker process survives. Mark the
			// task FAILED so a redelivery does not re-run a poison message
			// (handleAndExecute Ack-skips an already-FAILED task); Nack for
			// redelivery. The broker's redelivery limit handles deterministic
			// poison messages.
			//
			// The bookkeeping below reaches the DB, so it is guarded in turn: a
			// panic raised inside a deferred function is unrecoverable and would
			// take the worker process down — the exact outcome this handler
			// exists to prevent. taskCtx is read defensively as well, since a
			// panic is proof that an assumption upstream already broke.
			var task *entity.IngestionTask
			if taskCtx != nil {
				task = taskCtx.IngestionTask
			}
			taskID := ""
			if task != nil {
				taskID = task.ID
			}
			func() {
				defer func() {
					if r2 := recover(); r2 != nil {
						common.Error(fmt.Sprintf("task %s: failure handler panicked: %v", taskID, r2), fmt.Errorf("%v", r2))
					}
				}()
				common.Error(fmt.Sprintf("task %s panicked: %v", taskID, r), fmt.Errorf("%v", r))
				e.markFailed(ctx, taskID, fmt.Sprintf("Task panicked: %v", r))
				e.recordTerminalPipelineLog(ctx, task, string(entity.TaskStatusFail), fmt.Sprintf("Task panicked: %v", r))
			}()
			terminal = false
		}
		if e.leaseAbandoned(hb) {
			return
		}
		// Settlement authority is the DB, not the in-memory bool (BP1).
		// Fall back to the in-memory bool only when the DB is unavailable.
		if dbTerminal, ok := e.safeGetTerminal(ctx, taskCtx.IngestionTask.ID); ok {
			terminal = dbTerminal
		}
		e.ackOrNack(taskCtx, terminal)
	}()
	terminal = body(taskCtx.Ctx)
	return
}

// safeGetTerminal queries the DB for the task's actual status and returns
// whether it is terminal (COMPLETED/STOPPED/FAILED). A recover guards
// against nil-DB panics in test environments — in that case (false, false)
// is returned so the caller falls back to the in-memory bool.
func (e *Ingestor) safeGetTerminal(ctx context.Context, taskID string) (terminal bool, ok bool) {
	defer func() { recover() }()
	task, err := e.ingestionTaskSvc.GetTask(ctx, taskID)
	if err != nil {
		return false, false
	}
	return task.Status == common.COMPLETED ||
		task.Status == common.STOPPED ||
		task.Status == common.FAILED, true
}

// ackOrNack settles the MQ message according to the terminal flag: Ack if the
// task reached a durably-persisted terminal status, Nack otherwise so the
// broker redelivers and resumes. A nil handle (standalone/test path) is a no-op.
func (e *Ingestor) ackOrNack(taskCtx *taskpkg.TaskContext, terminal bool) {
	if taskCtx.Handle == nil {
		return
	}
	if terminal {
		if err := taskCtx.Handle.Ack(); err != nil {
			common.Error(fmt.Sprintf("ack task %s", taskCtx.IngestionTask.ID), err)
		}
		return
	}
	if err := taskCtx.Handle.Nack(); err != nil {
		common.Error(fmt.Sprintf("nack task %s", taskCtx.IngestionTask.ID), err)
	}
}

// defaultCancelCheck reads the Redis cancel flag that Python sets via
// REDIS_CONN.set(f"{task_id}-cancel", "x"). Falls back to checking the
// task status in DB when Redis is unavailable — a STOPPING status
// (set by RequestStop) is treated as a cancel signal.
func (e *Ingestor) defaultCancelCheck(ctx context.Context, taskID string) bool {
	rc := e.deps.KV
	if rc != nil {
		if ok, _ := rc.Exist(ctx, fmt.Sprintf("%s-cancel", taskID)); ok {
			return true
		}
	}
	task, err := e.ingestionTaskSvc.GetTask(ctx, taskID)
	if err != nil {
		return false
	}
	return task.Status == common.STOPPING
}

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
	// cancelCheck (e.g. stuck DB call) from blocking pollCancel itself,
	// which would cause executeTask's defer to deadlock on <-pollExited.
	checkOnce := func() <-chan bool {
		result := make(chan bool, 1)
		go func() {
			defer func() { recover() }() // goroutine may outlive pollCancel; must not crash process
			result <- e.cancelCheck(e.ctx, taskID)
		}()
		return result
	}

	// Initial check (immediately, for the test path).
	select {
	case <-done:
		return
	case ok := <-checkOnce():
		if ok {
			common.Info(fmt.Sprintf("Task %s cancel flag detected during polling, cancelling pipeline", taskID))
			cancel()
			return
		}
	}

	ticker := time.NewTicker(pollCancelInterval)
	defer ticker.Stop()
	for {
		select {
		case <-done:
			return
		case <-ticker.C:
			select {
			case <-done:
				return
			case ok := <-checkOnce():
				if ok {
					common.Info(fmt.Sprintf("Task %s cancel flag detected during polling, cancelling pipeline", taskID))
					cancel()
					return
				}
			}
		}
	}
}

// docRunFromTasks maps the doc's latest ingestion task to the document-level
// run label. tasks is a single-element slice holding the newest task (as
// returned by IngestionTaskDAO.GetByDocumentID, ordered by create_time DESC);
// tasks[0] is the current parse round and its status is authoritative. A
// document can be parsed multiple times over its lifetime, but document.run
// reflects the latest parse round, so historical tasks are ignored. An empty
// task set means UNSTART.
func docRunFromTasks(tasks []*entity.IngestionTask) string {
	if len(tasks) == 0 {
		return string(entity.TaskStatusUnstart)
	}
	latest := tasks[0]
	switch latest.Status {
	case common.CREATED, common.RUNNING, common.STOPPING:
		return string(entity.TaskStatusRunning)
	case common.FAILED:
		return string(entity.TaskStatusFail)
	case common.STOPPED:
		return string(entity.TaskStatusCancel)
	case common.COMPLETED:
		return string(entity.TaskStatusDone)
	default:
		return string(entity.TaskStatusUnstart)
	}
}

// claimTask registers a worker claim on a task ID. Returns false if another
// worker has already claimed it (e.g. MQ redelivery), true on first claim.
// The claim is released by releaseTask when the worker finishes, so a future
// redelivery (after restart) can re-claim the task. Broker lease renewal is a
// separate concern handled by the worker-owned Heartbeat until settlement.
func (e *Ingestor) claimTask(taskID string) bool {
	e.tasksMu.Lock()
	defer e.tasksMu.Unlock()
	if _, ok := e.currentTasks[taskID]; ok {
		return false
	}
	e.currentTasks[taskID] = struct{}{}
	return true
}

// releaseTask removes the claim so a future redelivery (after process restart)
// can re-claim the task.
func (e *Ingestor) releaseTask(taskID string) {
	e.tasksMu.Lock()
	delete(e.currentTasks, taskID)
	e.tasksMu.Unlock()
}

func (e *Ingestor) defaultRunDocumentTask(ctx context.Context, ingestionTask *entity.IngestionTask) error {
	const documentBulkSize = 32

	docTaskCtx, err := taskpkg.LoadFromIngestionTask(ctx, e.deps.DB, ingestionTask)
	if err != nil {
		return fmt.Errorf("load task context for %s: %w", ingestionTask.ID, err)
	}

	pipelineID := strings.TrimSpace(docTaskCtx.PipelineID)
	parserID := strings.TrimSpace(docTaskCtx.Doc.ParserID)
	isBuiltin := pipelineID == ""

	if pipelineID == "" {
		if parserID == "" {
			return fmt.Errorf("ingestion task %s: no pipeline_id or parser_id configured for document %s", ingestionTask.ID, docTaskCtx.Doc.ID)
		}
		pipelineID = parserID // builtin: parser_id acts as the logical pipeline identifier
	}

	docTaskCtx.Ctx = ctx
	// The sink owns all document/ingestion_task_log/ingestion_task.component_total
	// writes for this run; inject it into the executor so the pipeline reports
	// progress to the service layer instead of touching the DAO directly.
	// Keep the final document write bounded. Compiler products can contain large
	// text, source-chunk references, images, or vectors, so sending the whole
	// document through one Elasticsearch bulk request can exceed the request
	// size limit even when the individual chunks are valid.
	executor, err := taskpkg.NewPipelineExecutor(docTaskCtx, pipelineID, documentBulkSize, e.deps)
	if err != nil {
		return err
	}
	if isBuiltin {
		// Builtin path: load DSL from the embedded registry, skipping canvas DB lookup.
		executor.WithLoadDSLFunc(func(ctx context.Context, _ string) (string, string, error) {
			common.Info(fmt.Sprintf("load built in DSL for: %s", parserID))
			dsl, lerr := pipelinepkg.LoadBuiltinDSL(parserID)
			if lerr != nil {
				return "", "", lerr
			}
			return dsl, parserID, nil
		})
	}
	pipelineLogID := ""
	if ingestionTask.PipelineLogID != nil {
		pipelineLogID = *ingestionTask.PipelineLogID
	}
	sink := newProgressSink(ctx, e.ingestionTaskSvc, pipelineLogID)
	// Close is idempotent and intentionally called twice: the defer is the
	// panic backstop (an unwind out of Execute must still stop the flusher),
	// and the explicit call pins the final flush ahead of docState.apply,
	// whose terminal process_duration the flush would otherwise overwrite.
	defer sink.Close()
	result, err := executor.WithProgressSink(sink).Execute(docTaskCtx.Ctx)
	sink.Close()
	if err != nil {
		return err
	}
	e.stagePendingCompileEvent(ingestionTask.ID, docTaskCtx.Tenant.ID, result)
	e.docState.apply(ctx, result)
	return nil
}

func (e *Ingestor) stagePendingCompileEvent(taskID, tenantID string, result *taskpkg.PipelineResult) {
	if taskID == "" {
		return
	}
	e.compileEventsMu.Lock()
	defer e.compileEventsMu.Unlock()
	if result == nil || len(result.CompiledVariants) == 0 {
		delete(e.pendingCompileEvents, taskID)
		return
	}
	e.pendingCompileEvents[taskID] = pendingDocumentCompileEvent{
		tenantID:  tenantID,
		variants:  append([]string(nil), result.CompiledVariants...),
		taskTypes: append([]string(nil), result.CompiledTaskTypes...),
	}
}

func (e *Ingestor) clearPendingCompileEvent(taskID string) {
	e.compileEventsMu.Lock()
	delete(e.pendingCompileEvents, taskID)
	e.compileEventsMu.Unlock()
}

func (e *Ingestor) publishPendingCompileEvent(ctx context.Context, task *entity.IngestionTask) {
	if task == nil {
		return
	}
	e.compileEventsMu.Lock()
	event, ok := e.pendingCompileEvents[task.ID]
	delete(e.pendingCompileEvents, task.ID)
	e.compileEventsMu.Unlock()
	if !ok || len(event.variants) == 0 {
		return
	}
	if err := knowledge_compile.PublishCompleted(ctx, event.tenantID, task.DatasetID, task.DocumentID, event.variants, event.taskTypes); err != nil {
		common.Logger.Warn(fmt.Sprintf("knowledge_compile: publish doc_completed for %s failed: %v", task.DocumentID, err))
	}
}

func (e *Ingestor) recordTerminalPipelineLog(ctx context.Context, ingestionTask *entity.IngestionTask, status, message string) {
	if ingestionTask == nil || status == "" {
		return
	}
	ctx = context.WithoutCancel(ctx)
	if ingestionTask.PipelineLogID == nil || *ingestionTask.PipelineLogID == "" {
		common.Warn(fmt.Sprintf("record terminal pipeline log for task %s: missing run identity", ingestionTask.ID))
		return
	}
	run, err := dao.NewPipelineOperationLogDAO().GetByID(ctx, e.deps.DB, *ingestionTask.PipelineLogID)
	if err != nil {
		common.Warn(fmt.Sprintf("record terminal pipeline log for task %s: load run %s: %v", ingestionTask.ID, *ingestionTask.PipelineLogID, err))
		return
	}
	if run.DocumentID != ingestionTask.DocumentID || run.KbID != ingestionTask.DatasetID {
		common.Warn(fmt.Sprintf("record terminal pipeline log for task %s: run %s belongs to document %s/dataset %s, expected %s/%s", ingestionTask.ID, run.ID, run.DocumentID, run.KbID, ingestionTask.DocumentID, ingestionTask.DatasetID))
		return
	}
	// Keep the run's own binding: the document may have been re-bound since.
	pipelineID := ""
	if run.PipelineID != nil {
		pipelineID = strings.TrimSpace(*run.PipelineID)
	}
	versionID := ""
	if pipelineID != "" && run.PipelineVersionID != nil {
		versionID = strings.TrimSpace(*run.PipelineVersionID)
	}
	input := taskpkg.PipelineLogInput{
		KbID:              ingestionTask.DatasetID,
		DocumentID:        ingestionTask.DocumentID,
		PipelineID:        pipelineID,
		PipelineVersionID: versionID,
		Status:            status,
		PipelineLogID:     *ingestionTask.PipelineLogID,
	}
	if err := taskpkg.RecordPipelineLog(ctx, e.deps.DB, input); err != nil {
		common.Warn(fmt.Sprintf("record terminal pipeline log for task %s document %s: %v", ingestionTask.ID, ingestionTask.DocumentID, err))
		return
	}
	if err := e.ingestionTaskSvc.RecordTerminal(ctx, *ingestionTask.PipelineLogID, ingestionTask.ID, message); err != nil {
		common.Warn(fmt.Sprintf("record terminal event for task %s document %s: %v", ingestionTask.ID, ingestionTask.DocumentID, err))
	}
}

// Stop first stops worker registration and the dispatcher's Pull, then cancels
// execution. The caller's deadline bounds the wait for non-cooperative tasks.
func (e *Ingestor) Stop(ctx context.Context) {
	common.Info(fmt.Sprintf("Stopping ingestor %s", e.id))
	e.dispatchCancel()

	waitDone := make(chan struct{})
	go func() {
		e.dispatcherWg.Wait()
		e.cancel()
		e.workerWg.Wait()
		e.compileWg.Wait()
		e.memoryReconcileWg.Wait()
		close(waitDone)
	}()

	select {
	case <-waitDone:
		common.Info("All tasks completed")
	case <-ctx.Done():
		e.stopActiveLeases()
		e.cancel()
		e.tasksMu.RLock()
		ids := make([]string, 0, len(e.currentTasks))
		for id := range e.currentTasks {
			ids = append(ids, id)
		}
		e.tasksMu.RUnlock()
		common.Warn(fmt.Sprintf("Stop timed out with %d task(s) still in-flight (will be redelivered by broker): %v", len(ids), ids))
	}

	// Signal shutdown completion so the cmd-side select on <-ShutdownCh
	// unblocks (the admin graceful-shutdown path). Guarded by stopOnce: a
	// repeated Stop must not double-close the channel.
	e.stopOnce.Do(func() { close(e.ShutdownCh) })
}