Skip to content

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

feat(ingestion): add run-scoped progress events (#19775)

jay777211e7043413c32
diff --git a/internal/dao/database.go b/internal/dao/database.go
index 69d11a9659..deea385f24 100644
--- a/internal/dao/database.go
+++ b/internal/dao/database.go
@@ -181,6 +181,9 @@ func InitDB(ctx context.Context, migrateDB bool) error {
if err = RunMigrations(ctx, DB); err != nil {
return fmt.Errorf("failed to run manual migrations: %w", err)
}
+ if err = migrateIngestionLogRunIdentity(ctx, DB); err != nil {
+ return err
+ }
common.Info("Migrating database schema...")
for _, m := range dataModels {
@@ -197,6 +200,9 @@ func InitDB(ctx context.Context, migrateDB bool) error {
return fmt.Errorf("failed to migrate conversation history: %w", err)
}
} else {
+ if err = migrateIngestionLogRunIdentity(ctx, DB); err != nil {
+ return err
+ }
// Ensure the Go-exclusive runtime tables exist. The manual migrations are
// performed by the standalone --migrate action, so a server-mode process
// only converges the tables it needs itself.
diff --git a/internal/dao/database_test.go b/internal/dao/database_test.go
index d3fa2d1b1e..880a98a4a4 100644
--- a/internal/dao/database_test.go
+++ b/internal/dao/database_test.go
@@ -18,6 +18,7 @@ package dao
import (
"context"
+ "errors"
"strings"
"testing"
@@ -149,6 +150,96 @@ func TestMigrateIngestionTaskPipelineLogID(t *testing.T) {
}
}
+// TestMigrateIngestionLogRunIdentity verifies that legacy pipeline and event
+// tables gain the run identity columns without assigning a fake run number to
+// pre-existing rows. A NULL legacy run_count lets the unique document/run
+// index protect only newly allocated runs.
+func TestMigrateIngestionLogRunIdentity(t *testing.T) {
+ db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
+ if err != nil {
+ t.Fatalf("open sqlite: %v", err)
+ }
+ ctx := context.Background()
+ if err := db.Exec(`CREATE TABLE pipeline_operation_log (
+ id varchar(32) PRIMARY KEY,
+ document_id varchar(32) NOT NULL
+ )`).Error; err != nil {
+ t.Fatalf("create legacy pipeline log table: %v", err)
+ }
+ if err := db.Exec(`CREATE TABLE ingestion_task_log (
+ id integer PRIMARY KEY,
+ task_id varchar(32) NOT NULL,
+ checkpoint text NOT NULL,
+ phase integer,
+ component varchar(64),
+ message text
+ )`).Error; err != nil {
+ t.Fatalf("create legacy ingestion event table: %v", err)
+ }
+ if err := db.Exec(`INSERT INTO pipeline_operation_log (id, document_id) VALUES ('legacy-log', 'doc-1')`).Error; err != nil {
+ t.Fatalf("seed legacy pipeline log: %v", err)
+ }
+
+ if err := migrateIngestionLogRunIdentity(ctx, db); err != nil {
+ t.Fatalf("migrateIngestionLogRunIdentity: %v", err)
+ }
+ migrator := db.Migrator()
+ for _, column := range []string{"run_count"} {
+ if !migrator.HasColumn(&entity.PipelineOperationLog{}, column) {
+ t.Fatalf("pipeline_operation_log missing %s", column)
+ }
+ }
+ for _, column := range []string{"pipeline_log_id", "event_type"} {
+ if !migrator.HasColumn(&entity.IngestionTaskLog{}, column) {
+ t.Fatalf("ingestion_task_log missing %s", column)
+ }
+ }
+ if !migrator.HasIndex(&entity.IngestionTaskLog{}, "idx_ingestion_task_log_pipeline_id") {
+ t.Fatal("ingestion_task_log missing pipeline event index")
+ }
+ if !migrator.HasIndex(&entity.PipelineOperationLog{}, "idx_pipeline_operation_log_document_run") {
+ t.Fatal("pipeline_operation_log missing document run unique index")
+ }
+
+ var legacyRunCount *int
+ if err := db.Raw(`SELECT run_count FROM pipeline_operation_log WHERE id = 'legacy-log'`).Scan(&legacyRunCount).Error; err != nil {
+ t.Fatalf("read legacy run count: %v", err)
+ }
+ if legacyRunCount != nil {
+ t.Fatalf("legacy run_count = %d, want NULL", *legacyRunCount)
+ }
+ if err := db.Exec(`INSERT INTO pipeline_operation_log (id, document_id, run_count) VALUES ('run-1', 'doc-1', 1)`).Error; err != nil {
+ t.Fatalf("insert first numbered run: %v", err)
+ }
+ if err := db.Exec(`INSERT INTO pipeline_operation_log (id, document_id, run_count) VALUES ('run-2', 'doc-1', 1)`).Error; err == nil {
+ t.Fatal("duplicate numbered run was accepted")
+ }
+
+ if err := migrateIngestionLogRunIdentity(ctx, db); err != nil {
+ t.Fatalf("second migrateIngestionLogRunIdentity: %v", err)
+ }
+}
+
+type ingestionLogSchemaMigratorStub struct {
+ indexExistsAfterCreate bool
+}
+
+func (s *ingestionLogSchemaMigratorStub) HasIndex(any, string) bool {
+ return s.indexExistsAfterCreate
+}
+
+func (s *ingestionLogSchemaMigratorStub) CreateIndex(any, string) error {
+ s.indexExistsAfterCreate = true
+ return errors.New("connection closed after index creation")
+}
+
+func TestCreateIngestionLogSchemaObjectsRechecksAfterCreateError(t *testing.T) {
+ migrator := &ingestionLogSchemaMigratorStub{}
+ if err := createIngestionLogIndexIfMissing(migrator, &entity.IngestionTaskLog{}, "idx_ingestion_task_log_pipeline_id", "add ingestion_task_log pipeline index"); err != nil {
+ t.Fatalf("createIngestionLogIndexIfMissing: %v", err)
+ }
+}
+
func TestAutoMigrateRuntimeModelsNamesFailingTable(t *testing.T) {
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
diff --git a/internal/dao/document.go b/internal/dao/document.go
index f76d4858a1..582d53b0db 100644
--- a/internal/dao/document.go
+++ b/internal/dao/document.go
@@ -24,6 +24,7 @@ import (
"strings"
"gorm.io/gorm"
+ "gorm.io/gorm/clause"
)
// DocumentDAO document data access object
@@ -49,6 +50,18 @@ func (dao *DocumentDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*e
return &document, nil
}
+// GetByIDForUpdate fetches a document while holding the row lock used to
+// serialize creation of its ingestion-run identity. Callers must use a short
+// transaction and perform no external I/O while holding the lock.
+func (dao *DocumentDAO) GetByIDForUpdate(ctx context.Context, db *gorm.DB, id string) (*entity.Document, error) {
+ var document entity.Document
+ err := db.WithContext(ctx).Clauses(clause.Locking{Strength: "UPDATE"}).First(&document, "id = ?", id).Error
+ if err != nil {
+ return nil, err
+ }
+ return &document, nil
+}
+
// GetByAuthorID get documents by author ID
func (dao *DocumentDAO) GetByAuthorID(ctx context.Context, db *gorm.DB, authorID string, offset, limit int) ([]*entity.Document, int64, error) {
var documents []*entity.Document
@@ -156,7 +169,7 @@ func (dao *DocumentDAO) ListByKBIDWithOptions(ctx context.Context, db *gorm.DB,
// only the newest row, with ID as a deterministic tie-breaker for equal
// create times. This ordering must match IngestionTaskDAO's task lookups.
listQuery := db.WithContext(ctx).Table("document").
- Select(`document.*, user_canvas.title as pipeline_name, user.nickname, ingestion_task.status as ingestion_status`).
+ Select(`document.*, user_canvas.title as pipeline_name, user.nickname, ingestion_task.status as ingestion_status, ingestion_task.pipeline_log_id as pipeline_log_id`).
Joins("JOIN file2document ON file2document.document_id = document.id").
Joins("JOIN file ON file.id = file2document.file_id").
Joins("LEFT JOIN user_canvas ON document.pipeline_id = user_canvas.id").
diff --git a/internal/dao/ingestion.go b/internal/dao/ingestion.go
index fd8b1e375e..fc13ffa12a 100644
--- a/internal/dao/ingestion.go
+++ b/internal/dao/ingestion.go
@@ -88,6 +88,12 @@ func (dao *IngestionTaskDAO) UpdatePipelineLogID(ctx context.Context, db *gorm.D
return db.WithContext(ctx).Model(&entity.IngestionTask{}).Where("id = ?", taskID).Update("pipeline_log_id", logID).Error
}
+// ClearPipelineLogID detaches a terminal task from its completed run before a
+// user-initiated retry receives a new immutable run identity.
+func (dao *IngestionTaskDAO) ClearPipelineLogID(ctx context.Context, db *gorm.DB, taskID string) error {
+ return db.WithContext(ctx).Model(&entity.IngestionTask{}).Where("id = ?", taskID).Update("pipeline_log_id", nil).Error
+}
+
type TaskInfo struct {
TaskID string `json:"task_id"`
FilesToDelete []string `json:"files_to_delete"`
@@ -307,6 +313,25 @@ func (dao *IngestionTaskDAO) DeleteIfTerminal(ctx context.Context, db *gorm.DB,
type IngestionTaskLogDAO struct{}
+// IngestionEventPage is one keyset-paginated segment of an immutable run's
+// event stream. Events are always returned in ascending ID order so callers
+// can append or prepend them without re-sorting.
+type IngestionEventPage struct {
+ Events []*entity.IngestionTaskLog
+ HasMoreBefore bool
+ HasMoreAfter bool
+}
+
+// Event types stored in ingestion_task_log.event_type. Only lifecycle events
+// participate in component progress aggregation; the remaining kinds are the
+// immutable run event stream rendered by the UI.
+const (
+ EventTypeLifecycle = iota
+ EventTypeMessage
+ EventTypeTerminal
+ EventTypeSystem
+)
+
func NewIngestionTaskLogDAO() *IngestionTaskLogDAO {
return &IngestionTaskLogDAO{}
}
@@ -319,18 +344,144 @@ func (dao *IngestionTaskLogDAO) Update(ctx context.Context, db *gorm.DB, ingesti
return db.WithContext(ctx).Save(ingestionLog).Error
}
-// ListLogsByTaskID returns the task's logs in chronological (write) order.
-// Ordering is by auto-increment `id ASC` (NOT `create_time`) because
-// create_time has only second-level resolution and would tie-break
-// arbitrarily; `id` is monotonic and always reflects write order. This
-// feeds the frontend log stream (GET .../logs), which renders each row by
-// phase (0 started / 1 done / -1 failed).
-func (dao *IngestionTaskLogDAO) ListLogsByTaskID(ctx context.Context, db *gorm.DB, taskID string) ([]*entity.IngestionTaskLog, error) {
+// ListLogsByPipelineLogID returns one run's events in chronological write
+// order. The pipeline log id is the immutable run identity; task ids are
+// reusable across retries and must not be used to reconstruct a run.
+func (dao *IngestionTaskLogDAO) ListLogsByPipelineLogID(ctx context.Context, db *gorm.DB, pipelineLogID string) ([]*entity.IngestionTaskLog, error) {
var tasks []*entity.IngestionTaskLog
- err := db.WithContext(ctx).Where("task_id = ?", taskID).Order("id ASC").Find(&tasks).Error
+ err := db.WithContext(ctx).Where("pipeline_log_id = ?", pipelineLogID).Order("id ASC").Find(&tasks).Error
return tasks, err
}
+// LatestEventsByPipelineLogIDs returns each requested run's latest persisted
+// event in one query. It never falls back to task_id because a task can be
+// reused by a later run.
+func (dao *IngestionTaskLogDAO) LatestEventsByPipelineLogIDs(ctx context.Context, db *gorm.DB, pipelineLogIDs []string) (map[string]*entity.IngestionTaskLog, error) {
+ if len(pipelineLogIDs) == 0 {
+ return map[string]*entity.IngestionTaskLog{}, nil
+ }
+ latestIDs := db.WithContext(ctx).Model(&entity.IngestionTaskLog{}).
+ Select("MAX(id)").
+ Where("pipeline_log_id IN ?", pipelineLogIDs).
+ Group("pipeline_log_id")
+ var events []*entity.IngestionTaskLog
+ if err := db.WithContext(ctx).Where("id IN (?)", latestIDs).Find(&events).Error; err != nil {
+ return nil, err
+ }
+ result := make(map[string]*entity.IngestionTaskLog, len(events))
+ for _, event := range events {
+ if event != nil && event.PipelineLogID != nil && *event.PipelineLogID != "" {
+ result[*event.PipelineLogID] = event
+ }
+ }
+ return result, nil
+}
+
+// ListEventsPageByPipelineLogID returns one page for a run's immutable event
+// stream. afterID and beforeID are mutually exclusive keyset cursors; callers
+// validate public request parameters before invoking this DAO method.
+func (dao *IngestionTaskLogDAO) ListEventsPageByPipelineLogID(ctx context.Context, db *gorm.DB, pipelineLogID string, limit int, afterID, beforeID *int) (*IngestionEventPage, error) {
+ if limit <= 0 {
+ return nil, errors.New("ingestion event page limit must be positive")
+ }
+ if afterID != nil && beforeID != nil {
+ return nil, errors.New("ingestion event page cursors are mutually exclusive")
+ }
+
+ query := db.WithContext(ctx).Where("pipeline_log_id = ?", pipelineLogID)
+ descending := false
+ switch {
+ case afterID != nil:
+ query = query.Where("id > ?", *afterID).Order("id ASC")
+ case beforeID != nil:
+ query = query.Where("id < ?", *beforeID).Order("id DESC")
+ descending = true
+ default:
+ query = query.Order("id DESC")
+ descending = true
+ }
+
+ var events []*entity.IngestionTaskLog
+ if err := query.Limit(limit + 1).Find(&events).Error; err != nil {
+ return nil, err
+ }
+ page := &IngestionEventPage{}
+ if len(events) > limit {
+ if descending {
+ page.HasMoreBefore = true
+ } else {
+ page.HasMoreAfter = true
+ }
+ events = events[:limit]
+ }
+ if descending {
+ for left, right := 0, len(events)-1; left < right; left, right = left+1, right-1 {
+ events[left], events[right] = events[right], events[left]
+ }
+ }
+ page.Events = events
+
+ if len(events) == 0 {
+ switch {
+ case afterID != nil:
+ var err error
+ page.HasMoreBefore, err = dao.hasEventBeforeOrEqual(ctx, db, pipelineLogID, *afterID)
+ if err != nil {
+ return nil, err
+ }
+ case beforeID != nil:
+ var err error
+ page.HasMoreAfter, err = dao.hasEventAfterOrEqual(ctx, db, pipelineLogID, *beforeID)
+ if err != nil {
+ return nil, err
+ }
+ }
+ return page, nil
+ }
+
+ oldestID := events[0].ID
+ newestID := events[len(events)-1].ID
+ if !page.HasMoreBefore {
+ var err error
+ page.HasMoreBefore, err = dao.hasEventBefore(ctx, db, pipelineLogID, oldestID)
+ if err != nil {
+ return nil, err
+ }
+ }
+ if !page.HasMoreAfter {
+ var err error
+ page.HasMoreAfter, err = dao.hasEventAfter(ctx, db, pipelineLogID, newestID)
+ if err != nil {
+ return nil, err
+ }
+ }
+ return page, nil
+}
+
+func (dao *IngestionTaskLogDAO) hasEventBefore(ctx context.Context, db *gorm.DB, pipelineLogID string, id int) (bool, error) {
+ return dao.hasEvent(ctx, db, pipelineLogID, "id < ?", id)
+}
+
+func (dao *IngestionTaskLogDAO) hasEventAfter(ctx context.Context, db *gorm.DB, pipelineLogID string, id int) (bool, error) {
+ return dao.hasEvent(ctx, db, pipelineLogID, "id > ?", id)
+}
+
+func (dao *IngestionTaskLogDAO) hasEventBeforeOrEqual(ctx context.Context, db *gorm.DB, pipelineLogID string, id int) (bool, error) {
+ return dao.hasEvent(ctx, db, pipelineLogID, "id <= ?", id)
+}
+
+func (dao *IngestionTaskLogDAO) hasEventAfterOrEqual(ctx context.Context, db *gorm.DB, pipelineLogID string, id int) (bool, error) {
+ return dao.hasEvent(ctx, db, pipelineLogID, "id >= ?", id)
+}
+
+func (dao *IngestionTaskLogDAO) hasEvent(ctx context.Context, db *gorm.DB, pipelineLogID, condition string, id int) (bool, error) {
+ var count int64
+ err := db.WithContext(ctx).Model(&entity.IngestionTaskLog{}).
+ Where("pipeline_log_id = ? AND "+condition, pipelineLogID, id).
+ Limit(1).Count(&count).Error
+ return count > 0, err
+}
+
// TaskProgress is the server-side aggregate of a task's component progress,
// served by GET /api/v1/ingestion_task/{task_id}/progress so the frontend
// can render a progress bar without pulling the full log stream.
@@ -342,22 +493,13 @@ type TaskProgress struct {
Percent float64 `json:"percent"`
}
-// AggregateProgress computes {total, done, failed, running, percent} for a
-// task purely in SQL. It takes each component's latest row (max id per
-// component) and classifies by its phase:
-//
-// done = latest phase is exit/success (1)
-// failed = latest phase is error/failure (-1 legacy, or 2 after 1c)
-// running = anything else (started, 0)
-//
-// `total` is the authoritative denominator from ingestion_task.component_total.
-// The classification is forward-compatible with the §5.1 ProgressPhase
-// renumbering (exit=1 stays; error moves -1 -> 2).
-func (dao *IngestionTaskLogDAO) AggregateProgress(ctx context.Context, db *gorm.DB, taskID string, total int) (*TaskProgress, error) {
- // Latest row id per component for this task.
+// AggregateProgressByPipelineLogID computes component progress for one run.
+// Detailed messages and terminal/system events deliberately do not affect a
+// component's latest lifecycle state.
+func (dao *IngestionTaskLogDAO) AggregateProgressByPipelineLogID(ctx context.Context, db *gorm.DB, pipelineLogID string, total int) (*TaskProgress, error) {
latestIDs := db.WithContext(ctx).Model(&entity.IngestionTaskLog{}).
Select("MAX(id)").
- Where("task_id = ?", taskID).
+ Where("pipeline_log_id = ? AND event_type = ? AND component <> ?", pipelineLogID, EventTypeLifecycle, "").
Group("component")
type phaseRow struct {
@@ -373,11 +515,11 @@ func (dao *IngestionTaskLogDAO) AggregateProgress(ctx context.Context, db *gorm.
}
progress := &TaskProgress{Total: total}
- for _, r := range rows {
+ for _, row := range rows {
switch {
- case r.Phase == 1:
+ case row.Phase == 1:
progress.Done++
- case r.Phase < 0 || r.Phase == 2:
+ case row.Phase < 0 || row.Phase == 2:
progress.Failed++
default:
progress.Running++
@@ -389,39 +531,8 @@ func (dao *IngestionTaskLogDAO) AggregateProgress(ctx context.Context, db *gorm.
return progress, nil
}
-func (dao *IngestionTaskLogDAO) LatestLogByTaskID(ctx context.Context, db *gorm.DB, taskID string) (*entity.IngestionTaskLog, error) {
- var task *entity.IngestionTaskLog
- err := db.WithContext(ctx).Where("task_id = ?", taskID).Order("create_time DESC").First(&task).Error
- return task, err
-}
-
func (dao *IngestionTaskLogDAO) GetLogByLogID(ctx context.Context, db *gorm.DB, logID string) (*entity.IngestionTaskLog, error) {
var task *entity.IngestionTaskLog
err := db.WithContext(ctx).Where("id = ?", logID).First(&task).Error
return task, err
}
-
-func (dao *IngestionTaskLogDAO) DeleteByTaskID(ctx context.Context, db *gorm.DB, taskID string) (int64, error) {
- result := db.WithContext(ctx).Unscoped().Where("task_id = ?", taskID).Delete(&entity.IngestionTaskLog{})
- return result.RowsAffected, result.Error
-}
-
-// DeleteComponentLogsByTaskID removes component lifecycle rows from a new run
-// while preserving checkpoint rows such as run_count for task history.
-func (dao *IngestionTaskLogDAO) DeleteComponentLogsByTaskID(ctx context.Context, db *gorm.DB, taskID string) (int64, error) {
- var logs []*entity.IngestionTaskLog
- if err := db.WithContext(ctx).Where("task_id = ?", taskID).Find(&logs).Error; err != nil {
- return 0, err
- }
- ids := make([]int, 0, len(logs))
- for _, log := range logs {
- if log != nil && len(log.Checkpoint) == 0 {
- ids = append(ids, log.ID)
- }
- }
- if len(ids) == 0 {
- return 0, nil
- }
- result := db.WithContext(ctx).Unscoped().Where("id IN ?", ids).Delete(&entity.IngestionTaskLog{})
- return result.RowsAffected, result.Error
-}
diff --git a/internal/dao/ingestion_log_test.go b/internal/dao/ingestion_log_test.go
new file mode 100644
index 0000000000..349b1f003b
--- /dev/null
+++ b/internal/dao/ingestion_log_test.go
@@ -0,0 +1,157 @@
+//
+// 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 dao
+
+import (
+ "testing"
+
+ "ragflow/internal/entity"
+
+ "github.com/glebarez/sqlite"
+ "gorm.io/gorm"
+)
+
+func TestListEventsPageByPipelineLogIDUsesStableKeysets(t *testing.T) {
+ db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
+ if err != nil {
+ t.Fatalf("open sqlite: %v", err)
+ }
+ if err := db.AutoMigrate(&entity.IngestionTaskLog{}); err != nil {
+ t.Fatalf("migrate event table: %v", err)
+ }
+ for i := 1; i <= 5; i++ {
+ if err := db.Exec(`INSERT INTO ingestion_task_log
+ (task_id, pipeline_log_id, event_type, checkpoint, component, phase, message)
+ VALUES (?, ?, ?, '{}', '', 0, ?)`, "task-1", "run-1", EventTypeMessage, "event").Error; err != nil {
+ t.Fatalf("insert event %d: %v", i, err)
+ }
+ }
+
+ page, err := NewIngestionTaskLogDAO().ListEventsPageByPipelineLogID(t.Context(), db, "run-1", 2, nil, nil)
+ if err != nil {
+ t.Fatalf("list first page: %v", err)
+ }
+ assertEventIDs(t, page.Events, 4, 5)
+ if !page.HasMoreBefore || page.HasMoreAfter {
+ t.Fatalf("first page flags = before:%t after:%t, want true/false", page.HasMoreBefore, page.HasMoreAfter)
+ }
+
+ afterID := 2
+ page, err = NewIngestionTaskLogDAO().ListEventsPageByPipelineLogID(t.Context(), db, "run-1", 2, &afterID, nil)
+ if err != nil {
+ t.Fatalf("list after page: %v", err)
+ }
+ assertEventIDs(t, page.Events, 3, 4)
+ if !page.HasMoreBefore || !page.HasMoreAfter {
+ t.Fatalf("after page flags = before:%t after:%t, want true/true", page.HasMoreBefore, page.HasMoreAfter)
+ }
+
+ beforeID := 4
+ page, err = NewIngestionTaskLogDAO().ListEventsPageByPipelineLogID(t.Context(), db, "run-1", 2, nil, &beforeID)
+ if err != nil {
+ t.Fatalf("list before page: %v", err)
+ }
+ assertEventIDs(t, page.Events, 2, 3)
+ if !page.HasMoreBefore || !page.HasMoreAfter {
+ t.Fatalf("before page flags = before:%t after:%t, want true/true", page.HasMoreBefore, page.HasMoreAfter)
+ }
+
+ deletedCursor := 99
+ page, err = NewIngestionTaskLogDAO().ListEventsPageByPipelineLogID(t.Context(), db, "run-1", 2, &deletedCursor, nil)
+ if err != nil {
+ t.Fatalf("list with missing cursor: %v", err)
+ }
+ if len(page.Events) != 0 || !page.HasMoreBefore || page.HasMoreAfter {
+ t.Fatalf("missing-cursor page = %+v, want empty with only older history", page)
+ }
+}
+
+func TestLatestEventsByPipelineLogIDsReturnsOneRealRowPerRun(t *testing.T) {
+ db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
+ if err != nil {
+ t.Fatalf("open sqlite: %v", err)
+ }
+ if err := db.AutoMigrate(&entity.IngestionTaskLog{}); err != nil {
+ t.Fatalf("migrate event table: %v", err)
+ }
+ for _, runID := range []string{"run-1", "run-1", "run-2"} {
+ if err := db.Exec(`INSERT INTO ingestion_task_log
+ (task_id, pipeline_log_id, event_type, checkpoint, component, phase, message)
+ VALUES (?, ?, ?, '{}', '', 0, ?)`, "task-1", runID, EventTypeMessage, runID).Error; err != nil {
+ t.Fatalf("insert event for %s: %v", runID, err)
+ }
+ }
+
+ latest, err := NewIngestionTaskLogDAO().LatestEventsByPipelineLogIDs(t.Context(), db, []string{"run-1", "run-2", "missing"})
+ if err != nil {
+ t.Fatalf("LatestEventsByPipelineLogIDs: %v", err)
+ }
+ if len(latest) != 2 || latest["run-1"] == nil || latest["run-1"].ID != 2 || latest["run-2"] == nil || latest["run-2"].ID != 3 {
+ t.Fatalf("latest events = %+v, want IDs run-1=2 run-2=3", latest)
+ }
+}
+
+func assertEventIDs(t *testing.T, events []*entity.IngestionTaskLog, want ...int) {
+ t.Helper()
+ if len(events) != len(want) {
+ t.Fatalf("event count = %d, want %d (%v)", len(events), len(want), want)
+ }
+ for i, event := range events {
+ if event.ID != want[i] {
+ t.Fatalf("event id at %d = %d, want %d", i, event.ID, want[i])
+ }
+ }
+}
+
+func TestAggregateProgressByPipelineLogIDIgnoresMessagesAndOtherRuns(t *testing.T) {
+ db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
+ if err != nil {
+ t.Fatalf("open sqlite: %v", err)
+ }
+ if err := db.AutoMigrate(&entity.IngestionTaskLog{}); err != nil {
+ t.Fatalf("migrate event table: %v", err)
+ }
+ runOne := "run-1"
+ runTwo := "run-2"
+ events := []struct {
+ pipelineLogID string
+ eventType int
+ component string
+ phase int
+ message string
+ }{
+ {runOne, EventTypeLifecycle, "Parser", 1, ""},
+ {runOne, EventTypeMessage, "Parser", 0, "parser detail"},
+ {runOne, EventTypeLifecycle, "Chunker", 0, ""},
+ {runTwo, EventTypeLifecycle, "Parser", 1, ""},
+ }
+ for _, event := range events {
+ if err := db.Exec(`INSERT INTO ingestion_task_log
+ (task_id, pipeline_log_id, event_type, checkpoint, component, phase, message)
+ VALUES (?, ?, ?, '{}', ?, ?, ?)`, "task-1", event.pipelineLogID, event.eventType, event.component, event.phase, event.message).Error; err != nil {
+ t.Fatalf("create event: %v", err)
+ }
+ }
+
+ progress, err := NewIngestionTaskLogDAO().AggregateProgressByPipelineLogID(t.Context(), db, runOne, 2)
+ if err != nil {
+ t.Fatalf("aggregate progress: %v", err)
+ }
+ if progress.Done != 1 || progress.Running != 1 || progress.Failed != 0 || progress.Percent != 50 {
+ t.Fatalf("progress = %+v, want Done=1 Running=1 Failed=0 Percent=50", progress)
+ }
+}
diff --git a/internal/dao/migration.go b/internal/dao/migration.go
index 6e006c3efd..37dcb839f5 100644
--- a/internal/dao/migration.go
+++ b/internal/dao/migration.go
@@ -288,6 +288,70 @@ func migrateIngestionTaskPipelineLogID(ctx context.Context, db *gorm.DB) error {
return nil
}
+// migrateIngestionLogRunIdentity adds the columns and indexes that make each
+// ingestion event belong to one immutable pipeline-operation-log run. It is
+// deliberately explicit: the runtime startup path auto-migrates ingestion
+// tables and cannot be trusted to converge an existing schema safely.
+func migrateIngestionLogRunIdentity(ctx context.Context, db *gorm.DB) error {
+ migrator := db.WithContext(ctx).Migrator()
+ if migrator.HasTable(&entity.PipelineOperationLog{}) {
+ if !migrator.HasColumn(&entity.PipelineOperationLog{}, "run_count") {
+ if err := db.WithContext(ctx).Exec(
+ "ALTER TABLE pipeline_operation_log ADD COLUMN run_count int NULL",
+ ).Error; err != nil && !isDuplicateColumnError(err) {
+ return fmt.Errorf("add pipeline_operation_log.run_count: %w", err)
+ }
+ }
+ if err := createIngestionLogIndexIfMissing(migrator, &entity.PipelineOperationLog{}, "idx_pipeline_operation_log_document_run", "add pipeline_operation_log document/run index"); err != nil {
+ return err
+ }
+ }
+
+ if migrator.HasTable(&entity.IngestionTaskLog{}) {
+ if !migrator.HasColumn(&entity.IngestionTaskLog{}, "pipeline_log_id") {
+ if err := db.WithContext(ctx).Exec(
+ "ALTER TABLE ingestion_task_log ADD COLUMN pipeline_log_id varchar(32) NULL",
+ ).Error; err != nil && !isDuplicateColumnError(err) {
+ return fmt.Errorf("add ingestion_task_log.pipeline_log_id: %w", err)
+ }
+ }
+ if !migrator.HasColumn(&entity.IngestionTaskLog{}, "event_type") {
+ if err := db.WithContext(ctx).Exec(
+ "ALTER TABLE ingestion_task_log ADD COLUMN event_type tinyint NOT NULL DEFAULT 4",
+ ).Error; err != nil && !isDuplicateColumnError(err) {
+ return fmt.Errorf("add ingestion_task_log.event_type: %w", err)
+ }
+ }
+ if err := createIngestionLogIndexIfMissing(migrator, &entity.IngestionTaskLog{}, "idx_ingestion_task_log_pipeline_id", "add ingestion_task_log pipeline index"); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+type ingestionLogSchemaMigrator interface {
+ HasIndex(any, string) bool
+ CreateIndex(any, string) error
+}
+
+func createIngestionLogIndexIfMissing(migrator ingestionLogSchemaMigrator, model any, name, operation string) error {
+ if migrator.HasIndex(model, name) {
+ return nil
+ }
+ if err := migrator.CreateIndex(model, name); err != nil && !migrator.HasIndex(model, name) {
+ return fmt.Errorf("%s: %w", operation, err)
+ }
+ return nil
+}
+
+func isDuplicateColumnError(err error) bool {
+ if err == nil {
+ return false
+ }
+ text := err.Error()
+ return strings.Contains(text, "Error 1060") && strings.Contains(text, "Duplicate column name")
+}
+
// migrateKnowledgebaseNameUnique adds a case-insensitive unique constraint on
// (tenant_id, name) for valid knowledge bases. A VIRTUAL generated column
// (name_ci) computes LOWER(name) only for status='1' rows and is NULL otherwise,
diff --git a/internal/dao/order_by.go b/internal/dao/order_by.go
index d1f7209ff9..3cbc13186a 100644
--- a/internal/dao/order_by.go
+++ b/internal/dao/order_by.go
@@ -187,6 +187,7 @@ var pipelineLogOrderableColumns = map[string]struct{}{
"task_type": {},
"operation_status": {},
"status": {},
+ "run_count": {},
"create_time": {},
"create_date": {},
"update_time": {},
diff --git a/internal/dao/pipeline_operation_log.go b/internal/dao/pipeline_operation_log.go
index b7cdd551ac..6aedc7d7b5 100644
--- a/internal/dao/pipeline_operation_log.go
+++ b/internal/dao/pipeline_operation_log.go
@@ -18,10 +18,10 @@ package dao
import (
"context"
+ "fmt"
"strings"
"time"
- "ragflow/internal/common"
"ragflow/internal/entity"
"ragflow/internal/utility"
@@ -122,7 +122,7 @@ func (dao *PipelineOperationLogDAO) GetDatasetLogsByKBID(ctx context.Context, db
// documents share a name.
func (dao *PipelineOperationLogDAO) GetFileLogsByKBID(ctx context.Context, db *gorm.DB, kbID string, page, pageSize int, terms []OrderTerm, keywords, documentID string, operationStatus []string, createDateFrom, createDateTo string) ([]*entity.PipelineOperationLog, int64, error) {
query := db.WithContext(ctx).Model(&entity.PipelineOperationLog{}).
- Where("kb_id = ?", kbID)
+ Where("kb_id = ? AND run_count > 0", kbID)
if keywords != "" {
query = query.Where("LOWER(document_name) LIKE ?", "%"+strings.ToLower(keywords)+"%")
@@ -177,31 +177,6 @@ func OpenPipelineOperationStatuses() []string {
}
}
-// GetOpenLogByDocumentID returns the newest open pipeline operation log for a
-// document, or nil when the document has no in-flight run.
-//
-// It locates a row; it deliberately does not decide what may happen to it. The
-// create path drops whatever it finds (dropping a leftover from a run that no
-// longer exists), and the terminal writer adopts it only when the caller has no
-// bound row of its own. Whether a *terminal* write may touch a row is bound to
-// the run's own row id (ingestion_task.pipeline_log_id), so a superseded run
-// cannot reach the replacement run's row.
-func (dao *PipelineOperationLogDAO) GetOpenLogByDocumentID(ctx context.Context, db *gorm.DB, documentID string) (*entity.PipelineOperationLog, error) {
- var log entity.PipelineOperationLog
- err := db.WithContext(ctx).
- Where("document_id = ? AND operation_status IN ?", documentID, OpenPipelineOperationStatuses()).
- Order("create_time DESC").
- Order("id DESC").
- First(&log).Error
- if err != nil {
- if IsNotFoundErr(err) {
- return nil, nil
- }
- return nil, err
- }
- return &log, nil
-}
-
// OpenLogInput carries the bookkeeping needed to open or advance the
// pre-terminal row for a queued run. It mirrors the PipelineOperationLog
// columns that are known before the pipeline finishes; the DSL and the final
@@ -219,19 +194,22 @@ type OpenLogInput struct {
SourceFrom string
Avatar *string
OperationStatus string
- ProgressMsg string
+ RunCount int
}
-// CreateOpenLog opens the pre-terminal row for a queued run. Callers must
-// have verified no open row exists (or accept a best-effort duplicate on a
-// lost race); the error is returned so tests can assert it, while production
-// callers log and continue.
+// CreateOpenLog opens the pre-terminal row for a queued, numbered run. Its
+// caller holds the owning document row lock and binds the result to the task
+// before the task may be published.
func (dao *PipelineOperationLogDAO) CreateOpenLog(ctx context.Context, db *gorm.DB, input OpenLogInput) (*entity.PipelineOperationLog, error) {
+ if input.RunCount <= 0 {
+ return nil, fmt.Errorf("pipeline operation log run count must be positive")
+ }
now := time.Now().Local()
- msg := input.ProgressMsg
+ runCount := input.RunCount
log := &entity.PipelineOperationLog{
ID: utility.GenerateUUID(),
DocumentID: input.DocumentID,
+ RunCount: &runCount,
TenantID: input.TenantID,
KbID: input.KbID,
ParserID: input.ParserID,
@@ -240,7 +218,6 @@ func (dao *PipelineOperationLogDAO) CreateOpenLog(ctx context.Context, db *gorm.
DocumentType: input.DocumentType,
SourceFrom: input.SourceFrom,
Progress: 0,
- ProgressMsg: &msg,
ProcessBeginAt: &now,
ProcessDuration: 0,
DSL: entity.JSONMap{},
@@ -264,72 +241,36 @@ func (dao *PipelineOperationLogDAO) CreateOpenLog(ctx context.Context, db *gorm.
return log, nil
}
-// AdvanceOpenLog moves a run's own row to a later status, refreshing its
-// progress message. The row is targeted by id, so it can never touch another
-// run's row, and the update is guarded by the from-states the caller declares:
+// NextRunCount returns the next display number for a document. The caller
+// must already hold that document row FOR UPDATE, which makes MAX+1 safe
+// across concurrent enqueue requests without a separate counter table.
+func (dao *PipelineOperationLogDAO) NextRunCount(ctx context.Context, db *gorm.DB, documentID string) (int, error) {
+ var next int
+ err := db.WithContext(ctx).Model(&entity.PipelineOperationLog{}).
+ Where("document_id = ?", documentID).
+ Select("COALESCE(MAX(run_count), 0) + 1").
+ Scan(&next).Error
+ if err != nil {
+ return 0, err
+ }
+ return next, nil
+}
+
+// AdvanceOpenLog moves a run's own row to a later status. The row is targeted
+// by id, so it can never touch another run's row, and the update is guarded by
+// the from-states the caller declares:
// the transitions are monotonic (unstart -> schedule -> running), so a late
// queued write cannot regress a row a concurrent writer already advanced.
-// A row that already left the declared from-states is left untouched.
-func (dao *PipelineOperationLogDAO) AdvanceOpenLog(ctx context.Context, db *gorm.DB, logID string, fromStatuses []string, operationStatus, progressMsg string) error {
+// A row that already left the declared from-states is left untouched. The
+// legacy progress_msg column is intentionally not updated; run text belongs
+// to ingestion_task_log events.
+func (dao *PipelineOperationLogDAO) AdvanceOpenLog(ctx context.Context, db *gorm.DB, logID string, fromStatuses []string, operationStatus string) error {
if logID == "" || len(fromStatuses) == 0 {
return nil
}
return db.WithContext(ctx).Model(&entity.PipelineOperationLog{}).
Where("id = ? AND operation_status IN ?", logID, fromStatuses).
- Updates(map[string]interface{}{
- "operation_status": operationStatus,
- "progress_msg": progressMsg,
- }).Error
-}
-
-// DeleteOpenLogByID removes a run's own pre-terminal row. Used to clean up the
-// open row when a task is rolled back or removed, so the detail page is not
-// left with a permanently queued entry. Deleting by id (rather than by
-// document) keeps the cleanup away from a newer run's row, and the open-status
-// guard keeps it away from a terminal row, which is history.
-func (dao *PipelineOperationLogDAO) DeleteOpenLogByID(ctx context.Context, db *gorm.DB, logID string) error {
- if logID == "" {
- return nil
- }
- return db.WithContext(ctx).Model(&entity.PipelineOperationLog{}).
- Where("id = ? AND operation_status IN ?", logID, OpenPipelineOperationStatuses()).
- Delete(&entity.PipelineOperationLog{}).Error
-}
-
-// DeleteUnownedOpenLogByID removes an open row only when no live ingestion
-// task owns it. Existing databases may contain more than one task per document,
-// so document identity alone is not sufficient proof that a row is leftover.
-func (dao *PipelineOperationLogDAO) DeleteUnownedOpenLogByID(ctx context.Context, db *gorm.DB, logID string) (bool, error) {
- if logID == "" {
- return false, nil
- }
- result := db.WithContext(ctx).Model(&entity.PipelineOperationLog{}).
- Where("id = ? AND operation_status IN ?", logID, OpenPipelineOperationStatuses()).
- Where("NOT EXISTS (SELECT 1 FROM ingestion_task WHERE pipeline_log_id = ? AND status IN ?)", logID, common.ActiveTaskStatuses).
- Delete(&entity.PipelineOperationLog{})
- if result.Error != nil {
- return false, result.Error
- }
- return result.RowsAffected == 1, nil
-}
-
-// HasLiveTaskOwner reports whether a live ingestion task is bound to the given
-// pipeline operation log row. Adoption of an open row is gated on this: the
-// create path never takes a row a live run owns, and the terminal fallback must
-// apply the same rule, or it would finalize the live run's entry with another
-// run's status and swallow the live run's own terminal write.
-func (dao *PipelineOperationLogDAO) HasLiveTaskOwner(ctx context.Context, db *gorm.DB, logID string) (bool, error) {
- if logID == "" {
- return false, nil
- }
- var count int64
- err := db.WithContext(ctx).Model(&entity.IngestionTask{}).
- Where("pipeline_log_id = ? AND status IN ?", logID, common.ActiveTaskStatuses).
- Count(&count).Error
- if err != nil {
- return false, err
- }
- return count > 0, nil
+ Update("operation_status", operationStatus).Error
}
// GetByIDAndKBID fetches a single ingestion log scoped to its knowledge base.
diff --git a/internal/entity/document.go b/internal/entity/document.go
index 6f202283fa..95bc6a1180 100644
--- a/internal/entity/document.go
+++ b/internal/entity/document.go
@@ -69,6 +69,7 @@ type DocumentListItem struct {
ContentHash *string `gorm:"column:content_hash" json:"content_hash,omitempty"`
Suffix string `gorm:"column:suffix" json:"suffix"`
IngestionStatus *string `gorm:"column:ingestion_status" json:"ingestion_status,omitempty"`
+ PipelineLogID *string `gorm:"column:pipeline_log_id" json:"-"`
Status *string `gorm:"column:status" json:"status,omitempty"`
CreateTime *int64 `gorm:"column:create_time" json:"create_time,omitempty"`
CreateDate *time.Time `gorm:"column:create_date" json:"create_date,omitempty"`
diff --git a/internal/entity/ingestion_task.go b/internal/entity/ingestion_task.go
index 003bff98c2..8ab377e1bc 100644
--- a/internal/entity/ingestion_task.go
+++ b/internal/entity/ingestion_task.go
@@ -45,12 +45,14 @@ func (IngestionTask) TableName() string {
}
type IngestionTaskLog struct {
- ID int `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
- TaskID string `gorm:"column:task_id;size:32;not null;index" json:"task_id"`
- Checkpoint JSONMap `gorm:"column:checkpoint;type:longtext;not null" json:"checkpoint"`
- Phase int `gorm:"column:phase" json:"phase"`
- Component string `gorm:"column:component;size:64;index" json:"component"`
- Message string `gorm:"column:message;type:text" json:"message"`
+ ID int `gorm:"column:id;primaryKey;autoIncrement;index:idx_ingestion_task_log_pipeline_id,priority:2" json:"id"`
+ TaskID string `gorm:"column:task_id;size:32;not null;index" json:"task_id"`
+ PipelineLogID *string `gorm:"column:pipeline_log_id;size:32;index:idx_ingestion_task_log_pipeline_id,priority:1" json:"pipeline_log_id,omitempty"`
+ Checkpoint JSONMap `gorm:"column:checkpoint;type:longtext;not null" json:"checkpoint"`
+ Phase int `gorm:"column:phase" json:"phase"`
+ EventType int `gorm:"column:event_type;not null;default:4" json:"event_type"`
+ Component string `gorm:"column:component;size:64;index" json:"component"`
+ Message string `gorm:"column:message;type:text" json:"message"`
BaseModel
}
diff --git a/internal/entity/pipeline.go b/internal/entity/pipeline.go
index 21afee909b..a4b1bdf7a8 100644
--- a/internal/entity/pipeline.go
+++ b/internal/entity/pipeline.go
@@ -20,8 +20,11 @@ import "time"
// PipelineOperationLog pipeline operation log model
type PipelineOperationLog struct {
- ID string `gorm:"column:id;primaryKey;size:32" json:"id"`
- DocumentID string `gorm:"column:document_id;size:32;index" json:"document_id"`
+ ID string `gorm:"column:id;primaryKey;size:32" json:"id"`
+ DocumentID string `gorm:"column:document_id;size:32;index;uniqueIndex:idx_pipeline_operation_log_document_run,priority:1" json:"document_id"`
+ // RunCount is the monotonically allocated display number for a Go ingestion
+ // run. The Go read path only exposes positive run numbers.
+ RunCount *int `gorm:"column:run_count;uniqueIndex:idx_pipeline_operation_log_document_run,priority:2" json:"run_count,omitempty"`
TenantID string `gorm:"column:tenant_id;size:32;not null;index" json:"tenant_id"`
KbID string `gorm:"column:kb_id;size:32;not null;index" json:"kb_id"`
PipelineID *string `gorm:"column:pipeline_id;size:32;index" json:"pipeline_id,omitempty"`
diff --git a/internal/handler/dataset.go b/internal/handler/dataset.go
index 9b3b944f11..31a2382636 100644
--- a/internal/handler/dataset.go
+++ b/internal/handler/dataset.go
@@ -609,6 +609,56 @@ func (h *DatasetsHandler) GetIngestionLog(c *gin.Context) {
common.SuccessWithData(c, result, "success")
}
+// ListIngestionMessages handles GET
+// /api/v1/datasets/:dataset_id/ingestions/:log_id/messages.
+func (h *DatasetsHandler) ListIngestionMessages(c *gin.Context) {
+ user, errorCode, errorMessage := GetUser(c)
+ if errorCode != common.CodeSuccess {
+ common.ErrorWithCode(c, errorCode, errorMessage)
+ return
+ }
+
+ limit := 0
+ if rawLimit := c.Query("limit"); rawLimit != "" {
+ parsed, err := strconv.Atoi(rawLimit)
+ if err != nil || parsed <= 0 {
+ common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "limit must be a positive integer")
+ return
+ }
+ limit = parsed
+ }
+ afterID, ok := ingestionEventCursor(c, "after_id")
+ if !ok {
+ return
+ }
+ beforeID, ok := ingestionEventCursor(c, "before_id")
+ if !ok {
+ return
+ }
+
+ result, code, err := h.datasetsService.ListIngestionMessages(
+ c.Request.Context(), c.Param("dataset_id"), user.ID, c.Param("log_id"), limit, afterID, beforeID,
+ )
+ if err != nil {
+ common.ErrorWithCode(c, code, err.Error())
+ return
+ }
+ common.SuccessWithData(c, result, "success")
+}
+
+func ingestionEventCursor(c *gin.Context, name string) (*int, bool) {
+ raw := c.Query(name)
+ if raw == "" {
+ return nil, true
+ }
+ value, err := strconv.Atoi(raw)
+ if err != nil || value <= 0 {
+ common.ResponseWithCodeData(c, common.CodeArgumentError, nil, name+" must be a positive integer")
+ return nil, false
+ }
+ return &value, true
+}
+
// DeleteDatasets handles DELETE /api/v1/datasets.
func (h *DatasetsHandler) DeleteDatasets(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
diff --git a/internal/handler/document.go b/internal/handler/document.go
index f7844e2656..8743d53c74 100644
--- a/internal/handler/document.go
+++ b/internal/handler/document.go
@@ -86,6 +86,10 @@ type documentServiceIface interface {
HasActiveIngestionTasks(ctx context.Context, datasetID string) (bool, error)
}
+type latestIngestionEventLookup interface {
+ LatestIngestionEventsByPipelineLogIDs(ctx context.Context, pipelineLogIDs []string) (map[string]*service.IngestionEventItem, error)
+}
+
// fileUploadIface defines the FileService upload methods used by DocumentHandler.
type fileUploadIface interface {
UploadDocumentInfos(ctx context.Context, userID string, files []*multipart.FileHeader) ([]map[string]interface{}, common.ErrorCode, error)
@@ -623,6 +627,21 @@ func (h *DocumentHandler) ListDocuments(c *gin.Context) {
}
docs := make([]map[string]interface{}, 0, len(documents))
+ runIDs := make([]string, 0, len(documents))
+ for _, doc := range documents {
+ if doc != nil && doc.PipelineLogID != nil && *doc.PipelineLogID != "" {
+ runIDs = append(runIDs, *doc.PipelineLogID)
+ }
+ }
+ latestEventsByRun := map[string]*service.IngestionEventItem{}
+ if lookup, ok := h.documentService.(latestIngestionEventLookup); ok && len(runIDs) > 0 {
+ var lookupErr error
+ latestEventsByRun, lookupErr = lookup.LatestIngestionEventsByPipelineLogIDs(ctx, runIDs)
+ if lookupErr != nil {
+ common.Warn("failed to load latest ingestion events for document list", zap.Error(lookupErr))
+ latestEventsByRun = map[string]*service.IngestionEventItem{}
+ }
+ }
for _, doc := range documents {
if opts.CreateTimeFrom > 0 && doc.CreateTime != nil && *doc.CreateTime < opts.CreateTimeFrom {
continue
@@ -635,7 +654,11 @@ func (h *DocumentHandler) ListDocuments(c *gin.Context) {
metaFields = make(map[string]interface{})
}
- docs = append(docs, mapDocumentListItem(doc, metaFields))
+ var latestEvent *service.IngestionEventItem
+ if doc.PipelineLogID != nil {
+ latestEvent = latestEventsByRun[*doc.PipelineLogID]
+ }
+ docs = append(docs, mapDocumentListItem(doc, metaFields, latestEvent))
}
hasActiveTasks, err := h.documentService.HasActiveIngestionTasks(ctx, datasetID)
@@ -1152,7 +1175,7 @@ func (h *DocumentHandler) DownloadDocument(c *gin.Context) {
c.Data(http.StatusOK, res.ContentType, res.Data)
}
-func mapDocumentListItem(doc *entity.DocumentListItem, metaFields map[string]interface{}) map[string]interface{} {
+func mapDocumentListItem(doc *entity.DocumentListItem, metaFields map[string]interface{}, latestEvent *service.IngestionEventItem) map[string]interface{} {
processDuration := doc.ProcessDuration
if doc.IngestionStatus != nil && *doc.IngestionStatus == common.RUNNING && doc.ProcessBeginAt != nil {
processDuration = time.Since(*doc.ProcessBeginAt).Seconds()
@@ -1165,34 +1188,35 @@ func mapDocumentListItem(doc *entity.DocumentListItem, metaFields map[string]int
ingestionStatus = *doc.IngestionStatus
}
item := map[string]interface{}{
- "id": doc.ID,
- "dataset_id": doc.KbID,
- "name": stringValue(doc.Name),
- "thumbnail": stringValue(doc.Thumbnail),
- "size": doc.Size,
- "type": doc.Type,
- "created_by": doc.CreatedBy,
- "location": stringValue(doc.Location),
- "token_count": doc.TokenNum,
- "chunk_count": doc.ChunkNum,
- "progress": doc.Progress,
- "progress_msg": stringValue(doc.ProgressMsg),
- "process_begin_at": formatTimePtr(doc.ProcessBeginAt),
- "process_duration": processDuration,
- "suffix": doc.Suffix,
- "ingestion_status": ingestionStatus,
- "status": stringValue(doc.Status),
- "parser_id": doc.ParserID,
- "chunk_method": doc.ParserID,
- "pipeline_id": stringValue(doc.PipelineID),
- "pipeline_name": stringValue(doc.PipelineName),
- "nickname": stringValue(doc.Nickname),
- "parser_config": decodeJSONMap(string(doc.ParserConfig)),
- "meta_fields": metaFields,
- "create_time": int64(0),
- "create_date": "",
- "update_time": int64(0),
- "update_date": "",
+ "id": doc.ID,
+ "dataset_id": doc.KbID,
+ "name": stringValue(doc.Name),
+ "thumbnail": stringValue(doc.Thumbnail),
+ "size": doc.Size,
+ "type": doc.Type,
+ "created_by": doc.CreatedBy,
+ "location": stringValue(doc.Location),
+ "token_count": doc.TokenNum,
+ "chunk_count": doc.ChunkNum,
+ "progress": doc.Progress,
+ "progress_msg": stringValue(doc.ProgressMsg),
+ "latest_ingestion_event": latestEvent,
+ "process_begin_at": formatTimePtr(doc.ProcessBeginAt),
+ "process_duration": processDuration,
+ "suffix": doc.Suffix,
+ "ingestion_status": ingestionStatus,
+ "status": stringValue(doc.Status),
+ "parser_id": doc.ParserID,
+ "chunk_method": doc.ParserID,
+ "pipeline_id": stringValue(doc.PipelineID),
+ "pipeline_name": stringValue(doc.PipelineName),
+ "nickname": stringValue(doc.Nickname),
+ "parser_config": decodeJSONMap(string(doc.ParserConfig)),
+ "meta_fields": metaFields,
+ "create_time": int64(0),
+ "create_date": "",
+ "update_time": int64(0),
+ "update_date": "",
}
if doc.CreateTime != nil {
diff --git a/internal/handler/document_test.go b/internal/handler/document_test.go
index 318395ee88..39fbc1bf95 100644
--- a/internal/handler/document_test.go
+++ b/internal/handler/document_test.go
@@ -90,6 +90,15 @@ type fakeDocumentService struct {
hasActiveTasksErr error
}
+func TestMapDocumentListItemIncludesLatestIngestionEvent(t *testing.T) {
+ event := &service.IngestionEventItem{ID: 42, EventType: dao.EventTypeMessage, Message: "queued"}
+ item := mapDocumentListItem(&entity.DocumentListItem{ID: "doc-1", KbID: "kb-1"}, map[string]interface{}{}, event)
+ got, ok := item["latest_ingestion_event"].(*service.IngestionEventItem)
+ if !ok || got != event {
+ t.Fatalf("latest_ingestion_event = %#v, want original event", item["latest_ingestion_event"])
+ }
+}
+
func (f *fakeDocumentService) Ingest(ctx context.Context, userID string, req *document.IngestDocumentRequest) (common.ErrorCode, error) {
f.ingestUserID = userID
f.ingestReq = req
diff --git a/internal/handler/ingestion_messages_test.go b/internal/handler/ingestion_messages_test.go
new file mode 100644
index 0000000000..6fe0d9e810
--- /dev/null
+++ b/internal/handler/ingestion_messages_test.go
@@ -0,0 +1,139 @@
+package handler
+
+import (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+ "github.com/glebarez/sqlite"
+ "gorm.io/gorm"
+
+ "ragflow/internal/common"
+ "ragflow/internal/dao"
+ "ragflow/internal/entity"
+ dataset "ragflow/internal/service/dataset"
+)
+
+func TestDatasetsHandlerListIngestionMessagesValidatesAndPages(t *testing.T) {
+ db := setupIngestionMessagesHandlerDB(t)
+ insertIngestionMessagesHandlerKB(t, db, "kb-1", "user-1")
+ runCount := 1
+ if err := db.Create(&entity.PipelineOperationLog{
+ ID: "run-1",
+ DocumentID: "doc-1",
+ TenantID: "user-1",
+ KbID: "kb-1",
+ ParserID: "naive",
+ DocumentName: "doc.txt",
+ DocumentSuffix: ".txt",
+ DocumentType: "text",
+ SourceFrom: "local",
+ TaskType: string(entity.PipelineTaskTypeParse),
+ OperationStatus: string(entity.TaskStatusRunning),
+ RunCount: &runCount,
+ }).Error; err != nil {
+ t.Fatalf("insert run: %v", err)
+ }
+ for i := 0; i < 3; i++ {
+ if err := db.Model(&entity.IngestionTaskLog{}).Create(map[string]interface{}{
+ "task_id": "task-1",
+ "pipeline_log_id": "run-1",
+ "checkpoint": entity.JSONMap{},
+ "event_type": dao.EventTypeMessage,
+ "component": "",
+ "phase": 0,
+ "message": "detail",
+ }).Error; err != nil {
+ t.Fatalf("insert event: %v", err)
+ }
+ }
+
+ router := newIngestionMessagesHandlerRouter()
+ response := httptest.NewRecorder()
+ router.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/datasets/kb-1/ingestions/run-1/messages?limit=2", nil))
+ if response.Code != http.StatusOK {
+ t.Fatalf("status = %d, body = %s", response.Code, response.Body.String())
+ }
+ var body struct {
+ Code common.ErrorCode `json:"code"`
+ Data struct {
+ Items []struct {
+ ID int `json:"id"`
+ } `json:"items"`
+ OldestID int `json:"oldest_id"`
+ NewestID int `json:"newest_id"`
+ HasMoreBefore bool `json:"has_more_before"`
+ } `json:"data"`
+ }
+ if err := json.Unmarshal(response.Body.Bytes(), &body); err != nil {
+ t.Fatalf("unmarshal success response: %v", err)
+ }
+ if body.Code != common.CodeSuccess || len(body.Data.Items) != 2 || body.Data.Items[0].ID != 2 || body.Data.Items[1].ID != 3 || body.Data.OldestID != 2 || body.Data.NewestID != 3 || !body.Data.HasMoreBefore {
+ t.Fatalf("success response = %+v, want latest two events", body)
+ }
+
+ response = httptest.NewRecorder()
+ router.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/datasets/kb-1/ingestions/run-1/messages?after_id=1&before_id=2", nil))
+ if err := json.Unmarshal(response.Body.Bytes(), &body); err != nil {
+ t.Fatalf("unmarshal invalid response: %v", err)
+ }
+ if body.Code != common.CodeArgumentError {
+ t.Fatalf("invalid cursor code = %d, want %d", body.Code, common.CodeArgumentError)
+ }
+
+ response = httptest.NewRecorder()
+ router.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/datasets/kb-1/ingestions/run-1/messages?limit=0", nil))
+ if err := json.Unmarshal(response.Body.Bytes(), &body); err != nil {
+ t.Fatalf("unmarshal invalid limit response: %v", err)
+ }
+ if body.Code != common.CodeArgumentError {
+ t.Fatalf("zero limit code = %d, want %d", body.Code, common.CodeArgumentError)
+ }
+}
+
+func newIngestionMessagesHandlerRouter() *gin.Engine {
+ gin.SetMode(gin.TestMode)
+ h := NewDatasetsHandler(dataset.NewDatasetService(), nil)
+ r := gin.New()
+ r.GET("/api/v1/datasets/:dataset_id/ingestions/:log_id/messages", func(c *gin.Context) {
+ c.Set("user", &entity.User{ID: "user-1"})
+ h.ListIngestionMessages(c)
+ })
+ return r
+}
+
+func setupIngestionMessagesHandlerDB(t *testing.T) *gorm.DB {
+ t.Helper()
+ db, err := gorm.Open(sqlite.Open("file:"+url.QueryEscape(t.Name())+"?mode=memory&cache=shared"), &gorm.Config{TranslateError: true})
+ if err != nil {
+ t.Fatalf("open sqlite: %v", err)
+ }
+ if err := db.AutoMigrate(&entity.Knowledgebase{}, &entity.PipelineOperationLog{}, &entity.IngestionTaskLog{}, &entity.User{}, &entity.UserTenant{}); err != nil {
+ t.Fatalf("migrate test schema: %v", err)
+ }
+ original := dao.DB
+ dao.DB = db
+ t.Cleanup(func() { dao.DB = original })
+ return db
+}
+
+func insertIngestionMessagesHandlerKB(t *testing.T, db *gorm.DB, id, userID string) {
+ t.Helper()
+ status := string(entity.StatusValid)
+ if err := db.Create(&entity.Knowledgebase{
+ ID: id,
+ TenantID: userID,
+ Name: "messages",
+ EmbdID: "BAAI/bge-large-zh-v1.5@Builtin",
+ CreatedBy: userID,
+ Permission: string(entity.TenantPermissionMe),
+ ParserID: string(entity.ParserTypeNaive),
+ ParserConfig: entity.JSONMap{},
+ Status: &status,
+ }).Error; err != nil {
+ t.Fatalf("insert knowledgebase: %v", err)
+ }
+}
diff --git a/internal/handler/mcp_retrieval.go b/internal/handler/mcp_retrieval.go
index 78c05b0063..4c79cc708b 100644
--- a/internal/handler/mcp_retrieval.go
+++ b/internal/handler/mcp_retrieval.go
@@ -144,7 +144,7 @@ func MCPRetrieval(ctx context.Context, ds *dataset.DatasetService, userID string
if err != nil {
fields = map[string]any{}
}
- doc := mapDocumentListItem(docs[0], fields)
+ doc := mapDocumentListItem(docs[0], fields, nil)
metadata := map[string]any{"document_id": docID}
for _, key := range []string{"name", "location", "type", "size", "chunk_count", "create_date", "update_date", "token_count", "thumbnail", "dataset_id", "meta_fields"} {
metadata[key] = doc[key]
diff --git a/internal/ingestion/chunkcache/chunkcache.go b/internal/ingestion/chunkcache/chunkcache.go
index 5d8f62cc22..902775f0c7 100644
--- a/internal/ingestion/chunkcache/chunkcache.go
+++ b/internal/ingestion/chunkcache/chunkcache.go
@@ -168,22 +168,29 @@ func Set(ctx context.Context, s Store, key, value string) {
// PurgeTask drops every cache entry recorded for taskID, then the manifest
// itself. Call it once the task's chunks are durably persisted: from that point
// nothing can read the entries back, so holding them for the remaining TTL is
-// pure waste.
+// pure waste. A manifest read failure is returned because callers that remove
+// the task row must not make resumable cleanup look complete while Redis is
+// unavailable.
//
// The manifest is deleted last: losing it first would orphan the entries it
// still lists, leaving them to expire. Best-effort — a failed reclaim only
// means the entries wait out their TTL.
-func PurgeTask(ctx context.Context, s Store, taskID string) {
+func PurgeTask(ctx context.Context, s Store, taskID string) error {
if s == nil || taskID == "" {
- return
+ return nil
}
mk := manifestKey(taskID)
keys, err := s.SMembers(ctx, mk)
if err != nil {
- return
+ return fmt.Errorf("read chunk cache manifest %s: %w", mk, err)
}
for _, k := range keys {
- s.Delete(ctx, k)
+ if !s.Delete(ctx, k) {
+ return fmt.Errorf("delete chunk cache entry %s", k)
+ }
}
- s.Delete(ctx, mk)
+ if !s.Delete(ctx, mk) {
+ return fmt.Errorf("delete chunk cache manifest %s", mk)
+ }
+ return nil
}
diff --git a/internal/ingestion/chunkcache/chunkcache_test.go b/internal/ingestion/chunkcache/chunkcache_test.go
index dedc41165f..06a07527c7 100644
--- a/internal/ingestion/chunkcache/chunkcache_test.go
+++ b/internal/ingestion/chunkcache/chunkcache_test.go
@@ -37,6 +37,8 @@ type fakeStore struct {
deleted []string
failSet bool
expireFails bool
+ membersErr error
+ deleteFails map[string]bool
}
func newFakeStore() *fakeStore {
@@ -73,6 +75,9 @@ func (f *fakeStore) SAdd(_ context.Context, key, member string) bool {
}
func (f *fakeStore) SMembers(_ context.Context, key string) ([]string, error) {
+ if f.membersErr != nil {
+ return nil, f.membersErr
+ }
out := make([]string, 0, len(f.sets[key]))
for m := range f.sets[key] {
out = append(out, m)
@@ -83,6 +88,9 @@ func (f *fakeStore) SMembers(_ context.Context, key string) ([]string, error) {
func (f *fakeStore) Delete(_ context.Context, key string) bool {
f.deleted = append(f.deleted, key)
+ if f.deleteFails[key] {
+ return false
+ }
delete(f.kv, key)
delete(f.sets, key)
return true
@@ -276,6 +284,33 @@ func TestPurgeTask_NoopWithoutTaskOrStore(t *testing.T) {
PurgeTask(context.Background(), nil, "task-1")
}
+func TestPurgeTaskPropagatesEntryDeleteFailure(t *testing.T) {
+ f := newFakeStore()
+ ctx := taskCtx("task-1")
+ key := "kc:extractor:keywords:a"
+ Set(ctx, f, key, "result")
+ f.deleteFails = map[string]bool{key: true}
+
+ if err := PurgeTask(context.Background(), f, "task-1"); err == nil {
+ t.Fatal("PurgeTask error = nil, want entry delete failure")
+ }
+ if _, ok := f.sets[manifestKey("task-1")]; !ok {
+ t.Fatal("manifest was deleted after an entry delete failure")
+ }
+}
+
+func TestPurgeTaskPropagatesManifestReadError(t *testing.T) {
+ f := newFakeStore()
+ f.membersErr = errors.New("redis unavailable")
+
+ if err := PurgeTask(context.Background(), f, "task-1"); !errors.Is(err, f.membersErr) {
+ t.Fatalf("PurgeTask error = %v, want manifest read error", err)
+ }
+ if len(f.deleted) != 0 {
+ t.Fatalf("deleted = %v, want no deletes after manifest read failure", f.deleted)
+ }
+}
+
// TestKey_DiscriminatesEveryComponent asserts the key builder namespaces by
// kind and mixes every identity input, so a chat-model swap, a prompt edit or
// a different chunk can never read another entry's value.
diff --git a/internal/ingestion/component/knowledge_compiler/common/common_test.go b/internal/ingestion/component/knowledge_compiler/common/common_test.go
index e9b24023d3..f5250dce10 100644
--- a/internal/ingestion/component/knowledge_compiler/common/common_test.go
+++ b/internal/ingestion/component/knowledge_compiler/common/common_test.go
@@ -6,10 +6,32 @@ import (
"errors"
"strings"
"testing"
+ "time"
"ragflow/internal/agent/runtime"
)
+func TestRetryFailureReporterCoalescesRepeatedErrors(t *testing.T) {
+ reporter := RetryFailureReporter{}
+ err := errors.New("429 rate limit")
+
+ if message, ok := reporter.FailureMessage(1, 6, time.Second, err, false); !ok || !strings.Contains(message, "attempt 1/6") {
+ t.Fatalf("first failure = (%q, %v), want an emitted attempt message", message, ok)
+ }
+ if message, ok := reporter.FailureMessage(2, 6, 2*time.Second, err, false); ok || message != "" {
+ t.Fatalf("second repeated failure = (%q, %v), want suppressed", message, ok)
+ }
+ if message, ok := reporter.FailureMessage(3, 6, 4*time.Second, err, false); ok || message != "" {
+ t.Fatalf("third repeated failure = (%q, %v), want suppressed", message, ok)
+ }
+ if message, ok := reporter.FailureMessage(4, 6, 8*time.Second, err, false); !ok || !strings.Contains(message, "3 repeated failures") {
+ t.Fatalf("fourth failure = (%q, %v), want a coalesced retry update", message, ok)
+ }
+ if message, ok := reporter.RecoveryMessage(5); !ok || !strings.Contains(message, "recovered after 4 failures") {
+ t.Fatalf("recovery = (%q, %v), want recovery summary", message, ok)
+ }
+}
+
type captureChat struct{ req ChatRequest }
func (c *captureChat) Chat(_ context.Context, req ChatRequest) (*ChatResponse, error) {
diff --git a/internal/ingestion/component/knowledge_compiler/common/jsonchat.go b/internal/ingestion/component/knowledge_compiler/common/jsonchat.go
index 0c5b55ddf6..645d954fd4 100644
--- a/internal/ingestion/component/knowledge_compiler/common/jsonchat.go
+++ b/internal/ingestion/component/knowledge_compiler/common/jsonchat.go
@@ -57,13 +57,16 @@ func GenJSON(ctx context.Context, chat ChatInvoker, req ChatRequest, retryMax ..
req.DisableRetry = true
var lastErr error
delay := jsonRetryDelay
+ failureReporter := RetryFailureReporter{}
for attempt := 0; attempt <= maxRetries; attempt++ {
resp, err := chat.Chat(ctx, req)
if err != nil {
// Permanent chat errors (auth, unknown model, context-length,
// cancelled ctx) cannot succeed on a retry; escape immediately.
if !appcommon.IsTransientError(err) {
- reportLLMFailure(ctx, attempt, maxRetries, 0, err)
+ if message, ok := failureReporter.FailureMessage(attempt+1, maxRetries+1, 0, err, true); ok {
+ runtime.ReportProgressMessage(ctx, "Compiler", message)
+ }
return nil, err
}
// Transient chat failure (timeout / transport / provider); retry
@@ -72,6 +75,9 @@ func GenJSON(ctx context.Context, chat ChatInvoker, req ChatRequest, retryMax ..
} else {
if repaired, repairErr := RepairJSONText(resp.Content); repairErr == nil {
if m, unmarshalErr := tryUnmarshalJSONErr(repaired); unmarshalErr == nil {
+ if message, ok := failureReporter.RecoveryMessage(attempt + 1); ok {
+ runtime.ReportProgressMessage(ctx, "Compiler", message)
+ }
return m, nil
}
}
@@ -95,10 +101,14 @@ func GenJSON(ctx context.Context, chat ChatInvoker, req ChatRequest, retryMax ..
lastErr = fmt.Errorf("knowledge_compiler: LLM response is not parseable JSON (%d bytes)", len(resp.Content))
}
if attempt == maxRetries {
- reportLLMFailure(ctx, attempt, maxRetries, 0, lastErr)
+ if message, ok := failureReporter.FailureMessage(attempt+1, maxRetries+1, 0, lastErr, true); ok {
+ runtime.ReportProgressMessage(ctx, "Compiler", message)
+ }
break
}
- reportLLMFailure(ctx, attempt, maxRetries, delay, lastErr)
+ if message, ok := failureReporter.FailureMessage(attempt+1, maxRetries+1, delay, lastErr, false); ok {
+ runtime.ReportProgressMessage(ctx, "Compiler", message)
+ }
appcommon.Info("knowledge_compiler: GenJSON attempt failed, retrying",
zap.Int("attempt", attempt), zap.Duration("delay", delay),
zap.Error(lastErr))
@@ -157,14 +167,6 @@ func isJSONObjectOrArray(s string) bool {
return json.Valid([]byte(s))
}
-func reportLLMFailure(ctx context.Context, attempt, maxRetries int, delay time.Duration, err error) {
- message := fmt.Sprintf("[ERROR] LLM call failed (attempt %d/%d): %s", attempt+1, maxRetries+1, CompactError(err))
- if delay > 0 {
- message += fmt.Sprintf("; retrying in %s", delay)
- }
- runtime.ReportProgressMessage(ctx, "Compiler", message)
-}
-
// CompactError produces a bounded, single-line error suitable for progress
// messages. Provider errors may contain credentials, so redact common secret
// fields and API-key-shaped values before exposing the result to users.
diff --git a/internal/ingestion/component/knowledge_compiler/common/retry_progress.go b/internal/ingestion/component/knowledge_compiler/common/retry_progress.go
new file mode 100644
index 0000000000..bfa704252e
--- /dev/null
+++ b/internal/ingestion/component/knowledge_compiler/common/retry_progress.go
@@ -0,0 +1,63 @@
+package common
+
+import (
+ "fmt"
+ "time"
+)
+
+const retryFailureReportEvery = 3
+
+// RetryFailureReporter keeps transient retry noise from becoming one event per
+// provider attempt. It emits the first failure, then one coalesced update for
+// every retryFailureReportEvery repeated failures, and a final summary when
+// the caller exhausts retries.
+type RetryFailureReporter struct {
+ lastError string
+ failures int
+}
+
+// FailureMessage returns a user-facing progress message when this failure is
+// worth emitting. attempt is one-based and total is the maximum number of
+// attempts for the operation.
+func (r *RetryFailureReporter) FailureMessage(attempt, total int, delay time.Duration, err error, final bool) (string, bool) {
+ if r == nil {
+ return "", false
+ }
+ compact := CompactError(err)
+ if compact != r.lastError {
+ r.lastError = compact
+ r.failures = 1
+ return formatRetryFailure(attempt, total, delay, compact), true
+ }
+ r.failures++
+ if final {
+ return fmt.Sprintf("[ERROR] LLM call failed after %d attempts (%d repeated failures): %s", attempt, r.failures-1, compact), true
+ }
+ if r.failures > 1 && (r.failures-1)%retryFailureReportEvery == 0 {
+ return fmt.Sprintf("[ERROR] LLM call failed: %d repeated failures (latest attempt %d/%d): %s%s", r.failures-1, attempt, total, compact, retryDelaySuffix(delay)), true
+ }
+ return "", false
+}
+
+// RecoveryMessage returns one summary when a retry eventually succeeds after
+// repeated failures. Calling it resets the reporter for a later retry cycle.
+func (r *RetryFailureReporter) RecoveryMessage(attempt int) (string, bool) {
+ if r == nil || r.failures <= 1 {
+ return "", false
+ }
+ message := fmt.Sprintf("[INFO] LLM call recovered after %d failures on attempt %d", r.failures, attempt)
+ r.lastError = ""
+ r.failures = 0
+ return message, true
+}
+
+func formatRetryFailure(attempt, total int, delay time.Duration, compact string) string {
+ return fmt.Sprintf("[ERROR] LLM call failed (attempt %d/%d): %s%s", attempt, total, compact, retryDelaySuffix(delay))
+}
+
+func retryDelaySuffix(delay time.Duration) string {
+ if delay <= 0 {
+ return ""
+ }
+ return fmt.Sprintf("; retrying in %s", delay)
+}
diff --git a/internal/ingestion/component/knowledge_compiler/tree/claims.go b/internal/ingestion/component/knowledge_compiler/tree/claims.go
index 15bdb3d05c..5e612d1d2c 100644
--- a/internal/ingestion/component/knowledge_compiler/tree/claims.go
+++ b/internal/ingestion/component/knowledge_compiler/tree/claims.go
@@ -103,6 +103,46 @@ type claimEntry struct {
text string
}
+const claimProgressMilestones = 20
+
+type claimProgressReporter struct {
+ total int
+ step int
+ last int
+ nextTarget int
+}
+
+func newClaimProgressReporter(total int) *claimProgressReporter {
+ step := 1
+ if total > 0 {
+ step = (total + claimProgressMilestones - 1) / claimProgressMilestones
+ }
+ return &claimProgressReporter{total: total, step: step, nextTarget: step}
+}
+
+func (r *claimProgressReporter) shouldReport(completed int) bool {
+ if r == nil || completed <= 0 {
+ return false
+ }
+ if r.total > 0 && completed >= r.total {
+ if r.last == r.total {
+ return false
+ }
+ r.last = r.total
+ return true
+ }
+ if r.last == 0 {
+ r.last = completed
+ return true
+ }
+ if completed >= r.nextTarget {
+ r.last = completed
+ r.nextTarget = (completed/r.step + 1) * r.step
+ return true
+ }
+ return false
+}
+
// renderClaimSource builds the user prompt body for one claim-extraction call.
// Every chunk in the batch is a TARGET, so the model harvests claims for all of
// them in one call. Each chunk carries its own id, which both pins the claim
@@ -200,6 +240,7 @@ func ExtractClaimsForChunks(ctx context.Context, deps common.Deps, llmID string,
// and is not required to be goroutine-safe.
mu sync.Mutex
completed int
+ reporter = newClaimProgressReporter(total)
)
for i := range batches {
i := i
@@ -213,7 +254,9 @@ func ExtractClaimsForChunks(ctx context.Context, deps common.Deps, llmID string,
// which advances the counter only for finished batches; workers
// finish out of order, so the figure jumps by batch size).
completed += len(batch)
- runtime.ReportProgressMessage(ctx, "Compiler", fmt.Sprintf("tree-template: extracting claims for chunk %d/%d", completed, total))
+ if reporter.shouldReport(completed) {
+ runtime.ReportProgressMessage(ctx, "Compiler", fmt.Sprintf("tree-template: extracting claims for chunk %d/%d", completed, total))
+ }
mu.Unlock()
return nil
})
diff --git a/internal/ingestion/component/knowledge_compiler/tree/claims_test.go b/internal/ingestion/component/knowledge_compiler/tree/claims_test.go
index 29adaa2c3d..dae4675836 100644
--- a/internal/ingestion/component/knowledge_compiler/tree/claims_test.go
+++ b/internal/ingestion/component/knowledge_compiler/tree/claims_test.go
@@ -10,6 +10,22 @@ import (
"ragflow/internal/ingestion/component/knowledge_compiler/common"
)
+func TestClaimProgressReporterEmitsMilestonesAndFinalResult(t *testing.T) {
+ reporter := newClaimProgressReporter(100)
+ var reported []int
+ for completed := 1; completed <= 100; completed++ {
+ if reporter.shouldReport(completed) {
+ reported = append(reported, completed)
+ }
+ }
+ if len(reported) != 21 {
+ t.Fatalf("reported %d milestones, want 21: %v", len(reported), reported)
+ }
+ if reported[0] != 1 || reported[1] != 5 || reported[len(reported)-1] != 100 {
+ t.Fatalf("reported milestones = %v, want first 1, then 5-step milestones through 100", reported)
+ }
+}
+
// recordingEmbedder captures the texts it was asked to embed, so a test can
// assert what actually reached the vector.
type recordingEmbedder struct {
diff --git a/internal/ingestion/pipeline/cleanup.go b/internal/ingestion/pipeline/cleanup.go
new file mode 100644
index 0000000000..4cb11705f3
--- /dev/null
+++ b/internal/ingestion/pipeline/cleanup.go
@@ -0,0 +1,57 @@
+package pipeline
+
+import (
+ "context"
+ "errors"
+ "fmt"
+
+ "ragflow/internal/agent/canvas"
+ "ragflow/internal/ingestion/chunkcache"
+)
+
+// cleanupCheckpointState removes the checkpoint and the fingerprints that
+// describe the graph which produced it. It returns storage failures to
+// callers that need to gate a new run on cleanup completion; the pipeline's
+// cancellation path logs those failures and remains best-effort.
+func cleanupCheckpointState(ctx context.Context, store canvas.CheckPointStore, tracker *canvas.RunTracker, checkpointID string) error {
+ if checkpointID == "" {
+ return errors.New("checkpoint cleanup requires a checkpoint id")
+ }
+ var cleanupErr error
+ if store != nil {
+ for _, key := range []string{checkpointID, checkpointID + dslKeySuffix, checkpointID + ovfKeySuffix} {
+ if err := store.Delete(ctx, key); err != nil {
+ cleanupErr = errors.Join(cleanupErr, fmt.Errorf("delete %s: %w", key, err))
+ }
+ }
+ }
+ if tracker != nil {
+ if err := tracker.ClearInterruptID(ctx, checkpointID); err != nil {
+ cleanupErr = errors.Join(cleanupErr, fmt.Errorf("clear interrupt %s: %w", checkpointID, err))
+ }
+ }
+ return cleanupErr
+}
+
+// cleanupTaskState clears all resumable state associated with a task before
+// its terminal task row is removed for a rerun. Cache entries are purged only
+// after checkpoint cleanup succeeds so a failed cleanup remains retryable.
+func cleanupTaskState(ctx context.Context, store canvas.CheckPointStore, tracker *canvas.RunTracker, taskID string) error {
+ if err := cleanupCheckpointState(ctx, store, tracker, taskID); err != nil {
+ return err
+ }
+ if err := chunkcache.PurgeTask(ctx, chunkcache.Client(), taskID); err != nil {
+ return fmt.Errorf("purge chunk cache: %w", err)
+ }
+ return nil
+}
+
+// PurgeTaskState removes checkpoint, tracker, and per-chunk cache state for a
+// task using the process-wide Redis clients.
+func PurgeTaskState(ctx context.Context, taskID string) error {
+ if taskID == "" {
+ return errors.New("task state cleanup requires a task id")
+ }
+ p := &Pipeline{taskID: taskID}
+ return cleanupTaskState(ctx, p.resolveStore(), p.resolveTracker(), taskID)
+}
diff --git a/internal/ingestion/pipeline/pipeline.go b/internal/ingestion/pipeline/pipeline.go
index db04962863..8f0a047b0a 100644
--- a/internal/ingestion/pipeline/pipeline.go
+++ b/internal/ingestion/pipeline/pipeline.go
@@ -47,10 +47,8 @@ type Pipeline struct {
// these bytes, so a stale checkpoint can be detected and discarded
// instead of being resumed against an incompatible graph.
rawDSL []byte
- documentID string // owning document; progress is mirrored back to the
- // document table so the existing GET /api/v1/datasets/{dataset_id}/documents
- // endpoint (which reads document.progress/run/progress_msg) reflects the
- // live Go pipeline progress without a bespoke endpoint (plan §8).
+ documentID string // owning document; numeric progress is mirrored back to
+ // the document table for existing status projections.
canvas *canvas.Canvas
store canvas.CheckPointStore // optional injected; nil -> resolve at Run
tracker *canvas.RunTracker // optional injected; nil -> resolve at Run
@@ -99,9 +97,8 @@ func WithRequireResume() PipelineOption {
return func(p *Pipeline) { p.requireResume = true }
}
-// WithDocumentID binds the pipeline's owning document so progress can be
-// mirrored back into the document table (document.progress / run /
-// progress_msg) — the canonical store the document-list endpoint serves.
+// WithDocumentID binds the pipeline's owning document so numeric progress can
+// be mirrored back into the document table.
// Pass the empty string to disable the mirror (e.g. headless/test runs where
// the document row is not materialized).
func WithDocumentID(docID string) PipelineOption {
@@ -676,23 +673,8 @@ func (p *Pipeline) runResumable(ctx context.Context, runCtx context.Context, cur
// cleanupCheckpoint wipes the eino checkpoint payload and the persisted
// interrupt id (plan §4.3.b cancelled path).
func (p *Pipeline) cleanupCheckpoint(ctx context.Context, store canvas.CheckPointStore, tracker *canvas.RunTracker, cpID string) {
- if store != nil {
- if err := store.Delete(ctx, cpID); err != nil {
- common.Error(fmt.Sprintf("pipeline: delete checkpoint %s failed: %v", cpID, err), err)
- }
- // Drop the DSL / override fingerprints alongside the checkpoint so
- // they share one lifecycle on cancellation (otherwise the fingerprint
- // keys linger up to TTL while the checkpoint is gone — harmless, but
- // inconsistent). A later re-run overwrites them anyway.
- if err := store.Delete(ctx, cpID+dslKeySuffix); err != nil {
- common.Error(fmt.Sprintf("pipeline: delete DSL fingerprint %s failed: %v", cpID, err), err)
- }
- if err := store.Delete(ctx, cpID+ovfKeySuffix); err != nil {
- common.Error(fmt.Sprintf("pipeline: delete override fingerprint %s failed: %v", cpID, err), err)
- }
- }
- if tracker != nil {
- _ = tracker.ClearInterruptID(ctx, cpID)
+ if err := cleanupCheckpointState(ctx, store, tracker, cpID); err != nil {
+ common.Error(fmt.Sprintf("pipeline: cleanup checkpoint %s failed: %v", cpID, err), err)
}
}
diff --git a/internal/ingestion/pipeline/pipeline_test.go b/internal/ingestion/pipeline/pipeline_test.go
index bf74dacb89..8af84ed3ec 100644
--- a/internal/ingestion/pipeline/pipeline_test.go
+++ b/internal/ingestion/pipeline/pipeline_test.go
@@ -1064,6 +1064,26 @@ func TestCleanupCheckpoint_DeletesStoreAndClearsTracker(t *testing.T) {
}
}
+func TestCleanupTaskStateDeletesCheckpointAndFingerprints(t *testing.T) {
+ store := newMemCheckpointStore()
+ for _, key := range []string{"task-1", "task-1:dsl", "task-1:ovf"} {
+ if err := store.Set(t.Context(), key, []byte("state")); err != nil {
+ t.Fatalf("seed %s: %v", key, err)
+ }
+ }
+
+ if err := cleanupTaskState(t.Context(), store, nil, "task-1"); err != nil {
+ t.Fatalf("cleanupTaskState: %v", err)
+ }
+ for _, key := range []string{"task-1", "task-1:dsl", "task-1:ovf"} {
+ if _, ok, err := store.Get(t.Context(), key); err != nil {
+ t.Fatalf("read %s: %v", key, err)
+ } else if ok {
+ t.Fatalf("checkpoint state %s still exists", key)
+ }
+ }
+}
+
// =============================================================================
// runPlain — tracker integration with miniredis
// =============================================================================
diff --git a/internal/ingestion/service/execute_task_test.go b/internal/ingestion/service/execute_task_test.go
index 23bf2e18fe..eda3e6bc90 100644
--- a/internal/ingestion/service/execute_task_test.go
+++ b/internal/ingestion/service/execute_task_test.go
@@ -12,64 +12,6 @@ import (
"ragflow/internal/ingestion/testutil"
)
-// TestExecuteTask_CheckpointParseFailureDoesNotKillProcess verifies that checkpoint
-// parse failures do not call fatal exit (which would kill the whole worker process).
-// Instead, the task should be marked as FAILED and return gracefully.
-// This tests the fix for issue 1 from the code review.
-func TestExecuteTask_CheckpointParseFailureDoesNotKillProcess(t *testing.T) {
- db := testutil.SetupTestDB(t)
- cleanup := testutil.ReplaceDBForTest(t, db)
- defer cleanup()
-
- _, _, docID, taskID := testutil.SeedTestData(t, db,
- testutil.WithPipelineID("flow-1"),
- testutil.WithTenantID("tenant-1"),
- )
-
- // Create a task log with invalid checkpoint (run_count is a string instead of number)
- err := db.Create(&entity.IngestionTaskLog{
- TaskID: taskID,
- Checkpoint: entity.JSONMap{
- "run_count": "not-a-number", // intentionally wrong type
- },
- }).Error
- if err != nil {
- t.Fatalf("create bad task log: %v", err)
- }
-
- ingestor := newUnitIngestor("test", 1, []string{"pdf"})
- // Replace runDocumentTask to ensure it doesn't get called
- var runDocumentTaskCalled bool
- ingestor.runDocumentTask = func(ctx context.Context, ingestionTask *entity.IngestionTask) error {
- runDocumentTaskCalled = true
- return nil
- }
-
- taskCtx := taskpkg.NewTaskContextForScheduling(
- t.Context(),
- &entity.IngestionTask{ID: taskID, DocumentID: docID, DatasetID: "kb-1", Status: common.RUNNING},
- )
-
- // Execute the task - this should NOT panic or fatal exit (this is our main validation!)
- ctx := t.Context()
- ingestor.executeTask(ctx, taskCtx)
-
- // Corrupted run_count values are skipped by IncrementRunCount, so the task
- // proceeds to runDocumentTask and completes normally.
- if !runDocumentTaskCalled {
- t.Fatal("expected runDocumentTask to be called (bad run_count is skipped, not fatal)")
- }
-
- // Verify task status was set to COMPLETED
- finalTask, err := dao.NewIngestionTaskDAO().GetByID(ctx, db, taskID)
- if err != nil {
- t.Fatalf("load final ingestion task: %v", err)
- }
- if finalTask.Status != common.COMPLETED {
- t.Fatalf("final status = %s, want %s", finalTask.Status, common.COMPLETED)
- }
-}
-
func TestDefaultRunDocumentTask_BothPipelineAndParserMissing(t *testing.T) {
db := testutil.SetupTestDB(t)
cleanup := testutil.ReplaceDBForTest(t, db)
@@ -151,7 +93,6 @@ func TestExecuteTask_RunsDocumentTask(t *testing.T) {
testutil.WithPipelineID("flow-1"),
testutil.WithTenantID("tenant-1"),
)
-
ingestor := newUnitIngestor("test", 1, []string{"pdf"})
var runDocumentTaskCalled bool
var gotTaskID string
@@ -199,10 +140,8 @@ func TestExecuteTask_RunsDocumentTask(t *testing.T) {
}
// TestExecuteTask_CancelBeforePipeline verifies that when cancelCheck returns
-// true at task start, the task is cancelled before AdvanceStep,
-// runDocumentTask is never called, and document progress is set to -1 with a
-// cancel marker. Mirrors Python's cancel flow where has_canceled() returns
-// true in Pipeline.callback().
+// true at task start, the task is cancelled before the pipeline runs and the
+// legacy document progress message remains untouched.
func TestExecuteTask_CancelBeforePipeline(t *testing.T) {
db := testutil.SetupTestDB(t)
cleanup := testutil.ReplaceDBForTest(t, db)
@@ -211,6 +150,10 @@ func TestExecuteTask_CancelBeforePipeline(t *testing.T) {
testutil.WithPipelineID("flow-1"),
testutil.WithTenantID("tenant-1"),
)
+ const progressMessage = "Queued before cancellation"
+ if err := db.Model(&entity.Document{}).Where("id = ?", docID).Update("progress_msg", progressMessage).Error; err != nil {
+ t.Fatalf("seed document progress message: %v", err)
+ }
ingestor := newUnitIngestor("test", 1, []string{"pdf"})
ingestor.cancelCheck = func(ctx context.Context, taskID string) bool { return true }
@@ -239,7 +182,7 @@ func TestExecuteTask_CancelBeforePipeline(t *testing.T) {
if doc.Progress != -1 {
t.Fatalf("document.progress = %v, want -1 (cancelled)", doc.Progress)
}
- if doc.ProgressMsg == nil || *doc.ProgressMsg == "" {
- t.Fatal("document.progress_msg should contain cancel marker, got empty")
+ if doc.ProgressMsg == nil || *doc.ProgressMsg != progressMessage {
+ t.Fatalf("document.progress_msg = %v, want preserved %q", doc.ProgressMsg, progressMessage)
}
}
diff --git a/internal/ingestion/service/handle_and_execute_test.go b/internal/ingestion/service/handle_and_execute_test.go
index 9a7fbfbb5b..9d67e3cac6 100644
--- a/internal/ingestion/service/handle_and_execute_test.go
+++ b/internal/ingestion/service/handle_and_execute_test.go
@@ -23,6 +23,7 @@ import (
"time"
"ragflow/internal/common"
+ "ragflow/internal/dao"
"ragflow/internal/entity"
"ragflow/internal/ingestion/testutil"
servicepkg "ragflow/internal/service"
@@ -160,6 +161,130 @@ func TestHandleAndExecute_StartRunningTransientErrorNacks(t *testing.T) {
}
}
+func TestHandleAndExecute_InvalidRunIdentityFailsAndAcks(t *testing.T) {
+ db := testutil.SetupTestDB(t)
+ cleanup := testutil.ReplaceDBForTest(t, db)
+ defer cleanup()
+
+ _, _, _, taskID := testutil.SeedTestData(t, db)
+ if err := db.Model(&entity.IngestionTask{}).Where("id = ?", taskID).Update("pipeline_log_id", nil).Error; err != nil {
+ t.Fatalf("remove run binding: %v", err)
+ }
+ if err := db.Model(&entity.IngestionTask{}).Where("id = ?", taskID).Update("status", common.SCHEDULED).Error; err != nil {
+ t.Fatalf("schedule task: %v", err)
+ }
+
+ ingestor := newUnitIngestor("test-invalid-run-identity", 1, []string{"pdf"})
+ pipelineRan := false
+ ingestor.runDocumentTask = func(context.Context, *entity.IngestionTask) error {
+ pipelineRan = true
+ return nil
+ }
+ handle := &fakeTaskHandle{msg: common.TaskMessage{TaskID: taskID, TaskType: common.TaskTypeIngestionTask}}
+
+ ingestor.handleAndExecute(handle)
+
+ if pipelineRan {
+ t.Fatal("pipeline ran despite an invalid run identity")
+ }
+ if handle.acks.Load() != 1 || handle.nacks.Load() != 0 {
+ t.Fatalf("expected 1 Ack/0 Nack, got acks=%d nacks=%d", handle.acks.Load(), handle.nacks.Load())
+ }
+ var task entity.IngestionTask
+ if err := db.First(&task, "id = ?", taskID).Error; err != nil {
+ t.Fatalf("reload task: %v", err)
+ }
+ if task.Status != common.FAILED {
+ t.Fatalf("task status = %q, want FAILED", task.Status)
+ }
+}
+
+func TestHandleAndExecute_InvalidRunIdentitySettlesBoundRun(t *testing.T) {
+ db := testutil.SetupTestDB(t)
+ cleanup := testutil.ReplaceDBForTest(t, db)
+ defer cleanup()
+
+ _, _, _, taskID := testutil.SeedTestData(t, db)
+ if err := db.Model(&entity.IngestionTask{}).Where("id = ?", taskID).
+ Update("status", common.SCHEDULED).Error; err != nil {
+ t.Fatalf("schedule task: %v", err)
+ }
+ if err := db.Model(&entity.PipelineOperationLog{}).Where("id = ?", "run-"+taskID).
+ Update("run_count", 0).Error; err != nil {
+ t.Fatalf("invalidate run count: %v", err)
+ }
+
+ ingestor := newUnitIngestor("test-invalid-run-settle", 1, []string{"pdf"})
+ ingestor.runDocumentTask = func(context.Context, *entity.IngestionTask) error {
+ t.Fatal("pipeline ran despite an invalid run identity")
+ return nil
+ }
+ handle := &fakeTaskHandle{msg: common.TaskMessage{TaskID: taskID, TaskType: common.TaskTypeIngestionTask}}
+
+ ingestor.handleAndExecute(handle)
+
+ if handle.acks.Load() != 1 || handle.nacks.Load() != 0 {
+ t.Fatalf("expected 1 Ack/0 Nack, got acks=%d nacks=%d", handle.acks.Load(), handle.nacks.Load())
+ }
+ var run entity.PipelineOperationLog
+ if err := db.First(&run, "id = ?", "run-"+taskID).Error; err != nil {
+ t.Fatalf("reload pipeline log: %v", err)
+ }
+ if run.OperationStatus != string(entity.TaskStatusFail) {
+ t.Fatalf("pipeline log status = %q, want FAIL", run.OperationStatus)
+ }
+ var terminalCount int64
+ if err := db.Model(&entity.IngestionTaskLog{}).
+ Where("pipeline_log_id = ? AND event_type = ?", "run-"+taskID, dao.EventTypeTerminal).
+ Count(&terminalCount).Error; err != nil {
+ t.Fatalf("count terminal events: %v", err)
+ }
+ if terminalCount != 1 {
+ t.Fatalf("terminal events = %d, want 1", terminalCount)
+ }
+}
+
+func TestHandleAndExecute_InvalidRunIdentityDoesNotCloseForeignRun(t *testing.T) {
+ db := testutil.SetupTestDB(t)
+ cleanup := testutil.ReplaceDBForTest(t, db)
+ defer cleanup()
+
+ _, _, _, taskID := testutil.SeedTestData(t, db)
+ foreignRunCount := 1
+ if err := db.Create(&entity.PipelineOperationLog{
+ ID: "foreign-run",
+ DocumentID: "foreign-doc",
+ TenantID: "tenant-1",
+ KbID: "kb-1",
+ ParserID: "naive",
+ OperationStatus: string(entity.TaskStatusRunning),
+ RunCount: &foreignRunCount,
+ }).Error; err != nil {
+ t.Fatalf("create foreign run: %v", err)
+ }
+ if err := db.Model(&entity.IngestionTask{}).Where("id = ?", taskID).Updates(map[string]interface{}{
+ "status": common.SCHEDULED,
+ "pipeline_log_id": "foreign-run",
+ }).Error; err != nil {
+ t.Fatalf("corrupt task binding: %v", err)
+ }
+
+ ingestor := newUnitIngestor("test-foreign-run", 1, []string{"pdf"})
+ handle := &fakeTaskHandle{msg: common.TaskMessage{TaskID: taskID, TaskType: common.TaskTypeIngestionTask}}
+ ingestor.handleAndExecute(handle)
+
+ if handle.acks.Load() != 1 || handle.nacks.Load() != 0 {
+ t.Fatalf("expected 1 Ack/0 Nack, got acks=%d nacks=%d", handle.acks.Load(), handle.nacks.Load())
+ }
+ var run entity.PipelineOperationLog
+ if err := db.First(&run, "id = ?", "foreign-run").Error; err != nil {
+ t.Fatalf("reload foreign pipeline log: %v", err)
+ }
+ if run.OperationStatus != string(entity.TaskStatusRunning) {
+ t.Fatalf("foreign pipeline log status = %q, want RUNNING", run.OperationStatus)
+ }
+}
+
// TestHandleAndExecute_AlreadyTerminalTaskAcks verifies that tasks already in a
// terminal state (COMPLETED, STOPPED, FAILED) are Ack-skipped without re-execution.
func TestHandleAndExecute_AlreadyTerminalTaskAcks(t *testing.T) {
@@ -253,6 +378,14 @@ func TestHandleAndExecute_DocumentDuplicateClaimRenewsLease(t *testing.T) {
if !ingestor.claimTask(taskID) {
t.Fatal("first claim should succeed")
}
+ if err := db.Model(&entity.Document{}).Where("id = ?", "doc-1").Updates(map[string]interface{}{
+ "progress": 0.42,
+ "chunk_num": 7,
+ "token_num": 9,
+ "process_duration": 12.5,
+ }).Error; err != nil {
+ t.Fatalf("seed in-flight document state: %v", err)
+ }
pipelineRan := false
ingestor.runDocumentTask = func(ctx context.Context, task *entity.IngestionTask) error {
@@ -276,6 +409,13 @@ func TestHandleAndExecute_DocumentDuplicateClaimRenewsLease(t *testing.T) {
if handle.acks.Load() != 0 || handle.nacks.Load() != 0 {
t.Fatalf("expected 0 Ack/0 Nack for duplicate delivery, got acks=%d nacks=%d", handle.acks.Load(), handle.nacks.Load())
}
+ var doc entity.Document
+ if err := db.First(&doc, "id = ?", "doc-1").Error; err != nil {
+ t.Fatalf("reload document: %v", err)
+ }
+ if doc.Progress != 0.42 || doc.ChunkNum != 7 || doc.TokenNum != 9 || doc.ProcessDuration != 12.5 {
+ t.Fatalf("duplicate delivery rewrote document state: progress=%v chunks=%d tokens=%d duration=%v", doc.Progress, doc.ChunkNum, doc.TokenNum, doc.ProcessDuration)
+ }
}
// TestHandleAndExecute_MemoryUnsettledDispositionDefersToDurableRecovery
diff --git a/internal/ingestion/service/ingestion_service.go b/internal/ingestion/service/ingestion_service.go
index ac3e2c6796..13938256df 100644
--- a/internal/ingestion/service/ingestion_service.go
+++ b/internal/ingestion/service/ingestion_service.go
@@ -26,7 +26,6 @@ import (
"sync/atomic"
"time"
- "ragflow/internal/agent/canvas"
"ragflow/internal/common"
"ragflow/internal/dao"
"ragflow/internal/engine"
@@ -137,8 +136,6 @@ type Ingestor struct {
// cancel-flag lookup that mirrors Python's has_canceled(). Tests may
// override this to simulate cancel without Redis.
cancelCheck func(ctx context.Context, taskID string) bool
-
- checkpointExists func(ctx context.Context, taskID string) (bool, error)
}
type worker struct {
@@ -181,7 +178,6 @@ func NewIngestor(name string, maxConcurrency int32, supportedTypes []string) *In
ingestor.runMemoryTask = ingestor.defaultRunMemoryTask
ingestor.reconcileMemoryTasks = ingestor.defaultReconcileMemoryTasks
ingestor.cancelCheck = ingestor.defaultCancelCheck
- ingestor.checkpointExists = canvas.RedisCheckpointExists
ingestor.kcConcurrency = maxConcurrency // parallel dataset-level compile workers default to the task width
return ingestor
}
@@ -485,7 +481,7 @@ func (e *Ingestor) handleAndExecute(handle common.TaskHandle) {
return
}
- task, err := e.ingestionTaskSvc.StartRunning(e.ctx, taskMessage.TaskID)
+ 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))
@@ -508,12 +504,34 @@ func (e *Ingestor) handleAndExecute(handle common.TaskHandle) {
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) {
+ 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)
@@ -774,9 +792,9 @@ func (e *Ingestor) markFailed(ctx context.Context, taskID string) bool {
return true
}
-// runTask executes the task's business logic — run-count advance, document
-// pipeline, and completion — behind the heartbeat. It returns whether the
-// task reached a durably-persisted terminal status.
+// 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():
@@ -785,57 +803,17 @@ func (e *Ingestor) runTask(ctx context.Context, task *entity.IngestionTask) bool
return false
}
common.Info(fmt.Sprintf("Task %s cancelled", task.ID))
- e.markCancelProgress(task)
+ e.markTerminalProgress(task)
stopped := e.markStopped(context.Background(), task.ID)
if stopped {
- e.recordTerminalPipelineLog(context.Background(), task, string(entity.TaskStatusCancel))
+ e.recordTerminalPipelineLog(context.Background(), task, string(entity.TaskStatusCancel), "Task stopped by user.")
}
return stopped
default:
}
- // The three DB/Redis lookups below must survive a context cancelled
- // between the pre-check and the call (cancel poll races the pipeline
- // start): detach the caller's ctx with a short timeout so a cancelled
- // run fails through markFailed with a real error, not a context error.
- dbCtx, dbCancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
- defer dbCancel()
- if err := e.ingestionTaskSvc.IncrementRunCount(dbCtx, task.ID); err != nil {
- common.Error(fmt.Sprintf("Failed to increment run count for task %s", task.ID), err)
- ok := e.markFailed(ctx, task.ID)
- if ok {
- e.recordTerminalPipelineLog(ctx, task, string(entity.TaskStatusFail))
- }
- return ok
- }
- checkpointExists := e.checkpointExists
- if checkpointExists == nil {
- checkpointExists = canvas.RedisCheckpointExists
- }
- resumeCheckpoint, checkpointErr := checkpointExists(dbCtx, task.ID)
- if checkpointErr != nil {
- common.Error(fmt.Sprintf("Failed to check checkpoint for task %s", task.ID), checkpointErr)
- ok := e.markFailed(ctx, task.ID)
- if ok {
- e.recordTerminalPipelineLog(ctx, task, string(entity.TaskStatusFail))
- }
- return ok
- }
- if !resumeCheckpoint {
- if err := e.ingestionTaskSvc.ClearComponentProgress(dbCtx, task.ID); err != nil {
- common.Error(fmt.Sprintf("Failed to clear previous component progress for task %s", task.ID), err)
- ok := e.markFailed(ctx, task.ID)
- if ok {
- e.recordTerminalPipelineLog(ctx, task, string(entity.TaskStatusFail))
- }
- return ok
- }
- } else {
- common.Info(fmt.Sprintf("Preserving component progress for checkpoint resume of task %s", task.ID))
- }
-
- // This is a new run (IncrementRunCount succeeded). Any Redis cancel flag
- // that exists now is stale — a leftover from a previous run whose
+ // 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:
@@ -855,26 +833,26 @@ func (e *Ingestor) runTask(ctx context.Context, task *entity.IngestionTask) bool
return false
}
common.Info(fmt.Sprintf("Task %s cancelled during pipeline", task.ID))
- e.markCancelProgress(task)
+ e.markTerminalProgress(task)
stopped := e.markStopped(ctx, task.ID)
if stopped {
- e.recordTerminalPipelineLog(ctx, task, string(entity.TaskStatusCancel))
+ 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.markTimeoutProgress(task)
+ e.markTerminalProgress(task)
ok := e.markFailed(ctx, task.ID)
if ok {
- e.recordTerminalPipelineLog(ctx, task, string(entity.TaskStatusFail))
+ 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)
if ok {
- e.recordTerminalPipelineLog(ctx, task, string(entity.TaskStatusFail))
+ e.recordTerminalPipelineLog(ctx, task, string(entity.TaskStatusFail), fmt.Sprintf("Task failed: %v", err))
}
return ok
}
@@ -883,11 +861,26 @@ func (e *Ingestor) runTask(ctx context.Context, task *entity.IngestionTask) bool
common.Error(fmt.Sprintf("Task %s update status failed", task.ID), err)
return false
}
+ e.recordTerminalPipelineLog(ctx, task, string(entity.TaskStatusDone), "Task completed.")
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
@@ -967,6 +960,7 @@ func (e *Ingestor) settleMessage(ctx context.Context, taskCtx *taskpkg.TaskConte
// poison messages.
common.Error(fmt.Sprintf("task %s panicked: %v", taskCtx.IngestionTask.ID, r), fmt.Errorf("%v", r))
e.markFailed(ctx, taskCtx.IngestionTask.ID)
+ e.recordTerminalPipelineLog(ctx, taskCtx.IngestionTask, string(entity.TaskStatusFail), fmt.Sprintf("Task panicked: %v", r))
terminal = false
}
if e.leaseAbandoned(hb) {
@@ -1085,41 +1079,6 @@ func (e *Ingestor) pollCancel(taskID string, cancel context.CancelFunc, done <-c
}
}
-// markCancelProgress writes the cancelled-progress markers to the document
-// row: progress=-1 and an appended timestamped cancel message (progress_msg += cancelMsg).
-func (e *Ingestor) markCancelProgress(task *entity.IngestionTask) {
- svc := documentpkg.NewDocumentService()
- doc, err := svc.GetDocumentByID(e.ctx, task.DocumentID)
- if err != nil {
- common.Error(fmt.Sprintf("markCancelProgress: load document %s: %v", task.DocumentID, err), err)
- return
- }
- cancelMsg := fmt.Sprintf("\n%s Task stopped by user.", time.Now().Format("15:04:05"))
- existingMsg := ""
- if doc.ProgressMsg != nil {
- existingMsg = *doc.ProgressMsg
- }
- _ = svc.UpdateRunProgress(e.ctx, task.DocumentID, -1.0, existingMsg+cancelMsg)
-}
-
-// markTimeoutProgress writes the timeout-progress markers to the document
-// row. Unlike cancellation (markCancelProgress), this records a TIMEOUT
-// failure rather than a user-initiated stop.
-func (e *Ingestor) markTimeoutProgress(task *entity.IngestionTask) {
- svc := documentpkg.NewDocumentService()
- doc, err := svc.GetDocumentByID(e.ctx, task.DocumentID)
- if err != nil {
- common.Error(fmt.Sprintf("markTimeoutProgress: load document %s: %v", task.DocumentID, err), err)
- return
- }
- timeoutMsg := fmt.Sprintf("\n%s Task timed out.", time.Now().Format("15:04:05"))
- existingMsg := ""
- if doc.ProgressMsg != nil {
- existingMsg = *doc.ProgressMsg
- }
- _ = svc.UpdateRunProgress(e.ctx, task.DocumentID, -1.0, existingMsg+timeoutMsg)
-}
-
// 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
@@ -1179,7 +1138,11 @@ func (e *Ingestor) defaultRunDocumentTask(ctx context.Context, ingestionTask *en
return dsl, parserID, nil
})
}
- result, err := executor.WithRequireResume().WithProgressSink(newProgressSink(ctx, e.ingestionTaskSvc)).Execute(docTaskCtx.Ctx)
+ pipelineLogID := ""
+ if ingestionTask.PipelineLogID != nil {
+ pipelineLogID = *ingestionTask.PipelineLogID
+ }
+ result, err := executor.WithRequireResume().WithProgressSink(newProgressSink(ctx, e.ingestionTaskSvc, pipelineLogID)).Execute(docTaskCtx.Ctx)
if err != nil {
return err
}
@@ -1187,24 +1150,36 @@ func (e *Ingestor) defaultRunDocumentTask(ctx context.Context, ingestionTask *en
return nil
}
-func (e *Ingestor) recordTerminalPipelineLog(ctx context.Context, ingestionTask *entity.IngestionTask, status string) {
+func (e *Ingestor) recordTerminalPipelineLog(ctx context.Context, ingestionTask *entity.IngestionTask, status, message string) {
if ingestionTask == nil || status == "" {
return
}
ctx = context.WithoutCancel(ctx)
- input := taskpkg.PipelineLogInput{
- KbID: ingestionTask.DatasetID,
- DocumentID: ingestionTask.DocumentID,
- Status: status,
+ if ingestionTask.PipelineLogID == nil || *ingestionTask.PipelineLogID == "" {
+ common.Warn(fmt.Sprintf("record terminal pipeline log for task %s: missing run identity", ingestionTask.ID))
+ return
}
- // Bind the write to this run's own row. A superseded run keeps the id of
- // its (deleted or replaced) row, so its late terminal write can never reach
- // into the replacement run's row.
- if ingestionTask.PipelineLogID != nil {
- input.PipelineLogID = *ingestionTask.PipelineLogID
+ run, err := dao.NewPipelineOperationLogDAO().GetByID(ctx, dao.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
+ }
+ input := taskpkg.PipelineLogInput{
+ KbID: ingestionTask.DatasetID,
+ DocumentID: ingestionTask.DocumentID,
+ Status: status,
+ PipelineLogID: *ingestionTask.PipelineLogID,
}
if err := taskpkg.RecordPipelineLog(ctx, dao.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))
}
}
diff --git a/internal/ingestion/service/ingestor_lifecycle_test.go b/internal/ingestion/service/ingestor_lifecycle_test.go
index b30c3c9c53..275867fbdf 100644
--- a/internal/ingestion/service/ingestor_lifecycle_test.go
+++ b/internal/ingestion/service/ingestor_lifecycle_test.go
@@ -438,6 +438,9 @@ func TestStartSchedulesCreatedTasks(t *testing.T) {
db := testutil.SetupTestDB(t)
cleanup := testutil.ReplaceDBForTest(t, db)
defer cleanup()
+ if err := db.AutoMigrate(&entity.PipelineOperationLog{}); err != nil {
+ t.Fatalf("migrate pipeline operation log: %v", err)
+ }
_, _, _, taskID := testutil.SeedTestData(t, db)
if err := db.Model(&entity.IngestionTask{}).Where("id = ?", taskID).
diff --git a/internal/ingestion/service/progress_sink.go b/internal/ingestion/service/progress_sink.go
index 41846787df..35c117d80f 100644
--- a/internal/ingestion/service/progress_sink.go
+++ b/internal/ingestion/service/progress_sink.go
@@ -18,12 +18,8 @@ package service
import (
"context"
- "errors"
"fmt"
- "strings"
- "sync"
"sync/atomic"
- "time"
"ragflow/internal/common"
"ragflow/internal/dao"
@@ -32,33 +28,19 @@ import (
documentpkg "ragflow/internal/service/document"
)
-// progressLogMaxChars bounds the accumulated run log mirrored into
-// document.progress_msg.
-const progressLogMaxChars = 3000
-
-// progressSink implements pipeline.ProgressSink. It is the single writer of
-// the document / ingestion_task_log / ingestion_task.component_total tables
-// for a pipeline run: the pipeline reports component lifecycle events here
-// and the sink persists them through the service layer (IngestionTaskService
-// + DocumentService), never the DAO directly. All writes are best-effort -
-// failures are logged and never abort the run, mirroring the legacy
-// pipeline-internal sink semantics.
+// progressSink implements pipeline.ProgressSink. It records a run's component
+// events and total through the service layer, and mirrors only the numeric
+// progress state to the owning document. All writes are best-effort so a
+// reporting failure never aborts the pipeline run.
type progressSink struct {
- taskSvc *servicepkg.IngestionTaskService
- docSvc docProgressSvc
+ taskSvc *servicepkg.IngestionTaskService
+ docSvc docProgressSvc
+ pipelineLogID string
// total is the component-count denominator cached from OnComponentTotal.
// It is Store-d once in the Run goroutine and Load-ed by OnComponentProgress,
// which eino fires from concurrent parallel-branch goroutines. Atomic because
// the two access paths share no other synchronization.
total atomic.Int64
- // logMu guards the accumulated run log below: OnComponentProgress fires
- // from concurrent parallel-branch goroutines, so both the lazy seed and
- // the append must hold the same mutex.
- logMu sync.Mutex
- seeded bool
- log strings.Builder
- pending []string
- now func() time.Time
}
// docProgressSvc is the subset of *service.DocumentService the sink needs to
@@ -66,21 +48,19 @@ type progressSink struct {
// tests can inject a stub and assert the mirror call without depending on the
// full DocumentService surface.
type docProgressSvc interface {
- GetDocumentByID(ctx context.Context, docID string) (*documentpkg.DocumentResponse, error)
UpdateRunState(ctx context.Context, docID string, progress float64) error
- UpdateRunProgress(ctx context.Context, docID string, progress float64, progressMsg string) error
}
-func newProgressSink(ctx context.Context, taskSvc *servicepkg.IngestionTaskService) *progressSink {
+func newProgressSink(ctx context.Context, taskSvc *servicepkg.IngestionTaskService, pipelineLogID string) *progressSink {
// Eagerly construct the DocumentService so docSvc is immutable after this
// point. eino's compose graph runs parallel branches concurrently, so
// OnComponentProgress (and thus docSvc) can fire from multiple goroutines;
// a lazy check-then-act here would be a data race. The sink owns no
// server-config dependency, so this is safe in any environment.
return &progressSink{
- taskSvc: taskSvc,
- docSvc: documentpkg.NewDocumentService(),
- now: time.Now,
+ taskSvc: taskSvc,
+ docSvc: documentpkg.NewDocumentService(),
+ pipelineLogID: pipelineLogID,
}
}
@@ -92,14 +72,14 @@ func (s *progressSink) OnComponentTotal(ctx context.Context, taskID string, tota
}
func (s *progressSink) OnComponentProgress(ctx context.Context, ev pipeline.ProgressEvent) {
- if err := s.taskSvc.RecordComponentProgress(ctx, ev.TaskID, ev.Component, ev.Phase, ev.Message); err != nil {
+ if err := s.taskSvc.RecordLifecycle(ctx, s.pipelineLogID, ev.TaskID, ev.Component, ev.Phase, ev.Message); err != nil {
common.Error(fmt.Sprintf("progressSink: record component progress for task %s failed: %v", ev.TaskID, err), err)
}
if ev.DocumentID == "" {
return
}
total := s.total.Load()
- agg, err := s.taskSvc.AggregateTaskProgress(ctx, ev.TaskID, int(total))
+ agg, err := s.taskSvc.AggregateTaskProgressByPipelineLogID(ctx, s.pipelineLogID, int(total))
if err != nil {
common.Error(fmt.Sprintf("progressSink: aggregate task progress for task %s failed: %v", ev.TaskID, err), err)
return
@@ -108,126 +88,21 @@ func (s *progressSink) OnComponentProgress(ctx context.Context, ev pipeline.Prog
return
}
progress := deriveDocumentProgress(agg, int(total))
- if err = s.updateDocumentProgress(ctx, ev.DocumentID, progress, ev.Message); err != nil {
- common.Error(fmt.Sprintf("progressSink: mirror progress to document %s for task %s failed: %v", ev.DocumentID, ev.TaskID, err), err)
+ if err = s.docSvc.UpdateRunState(ctx, ev.DocumentID, progress); err != nil {
+ common.Error(fmt.Sprintf("progressSink: update progress state for document %s task %s failed: %v", ev.DocumentID, ev.TaskID, err), err)
}
}
-// OnComponentMessage appends detailed compiler-stage information without
-// creating a lifecycle row. This keeps component_total and completion percent
-// based on actual canvas components while exposing MAP/REDUCE/PLAN/REFINE
-// counts through the existing document progress log.
-func (s *progressSink) OnComponentMessage(ctx context.Context, taskID, docID, component, message string) {
- if docID == "" || message == "" {
- return
- }
- s.logMu.Lock()
- defer s.logMu.Unlock()
- doc, err := s.docSvc.GetDocumentByID(ctx, docID)
- if err != nil {
- common.Error(fmt.Sprintf("progressSink: read document %s for detail failed", docID), err)
- return
- }
- if doc == nil {
- return
- }
- log, err := s.accumulateLogLocked(ctx, docID, fmt.Sprintf("%s: %s", component, message))
- if err != nil {
- common.Error(fmt.Sprintf("progressSink: append detail for document %s failed", docID), err)
+// OnComponentMessage records detailed compiler-stage information without
+// creating a lifecycle row, so completion percent remains based on actual
+// canvas components.
+func (s *progressSink) OnComponentMessage(ctx context.Context, taskID, _ string, component, message string) {
+ if message == "" {
return
}
- if err := s.docSvc.UpdateRunProgress(ctx, docID, doc.Progress, log); err != nil {
- common.Error(fmt.Sprintf("progressSink: persist detail for document %s failed", docID), err)
- }
-}
-
-// updateDocumentProgress serializes log accumulation with the corresponding
-// document write. Without holding the same lock across both operations, a
-// slower database write can store an older snapshot after a newer event has
-// already been persisted.
-func (s *progressSink) updateDocumentProgress(ctx context.Context, docID string, progress float64, msg string) error {
- s.logMu.Lock()
- defer s.logMu.Unlock()
- log, err := s.accumulateLogLocked(ctx, docID, msg)
- if err != nil {
- // Keep the current run state durable even when the existing log cannot
- // be seeded. The next event will retry the seed and persist the log.
- stateErr := s.docSvc.UpdateRunState(ctx, docID, progress)
- return errors.Join(err, stateErr)
- }
- return s.docSvc.UpdateRunProgress(ctx, docID, progress, log)
-}
-
-// accumulateLog appends one component lifecycle line to the run log. It is
-// retained as a small test seam; production writes use updateDocumentProgress
-// so the append and document update share one lock.
-func (s *progressSink) accumulateLog(ctx context.Context, docID, msg string) string {
- s.logMu.Lock()
- defer s.logMu.Unlock()
- log, _ := s.accumulateLogLocked(ctx, docID, msg)
- return log
-}
-
-// accumulateLogLocked appends one component lifecycle line to the run log and
-// returns the full accumulated log, or an error when the existing log could
-// not be seeded. The caller must hold logMu.
-func (s *progressSink) accumulateLogLocked(ctx context.Context, docID, msg string) (string, error) {
- if !s.seeded {
- doc, err := s.docSvc.GetDocumentByID(ctx, docID)
- if err != nil {
- if msg != "" {
- s.pending = append(s.pending, msg)
- }
- common.Warn(fmt.Sprintf("progressSink: seed run log from document %s: %v", docID, err))
- return "", fmt.Errorf("seed run log for document %s: %w", docID, err)
- }
- s.seeded = true
- if doc != nil && doc.ProgressMsg != nil {
- s.log.WriteString(*doc.ProgressMsg)
- }
- for _, pending := range s.pending {
- s.appendLogLineLocked(pending)
- }
- s.pending = nil
- }
- if msg == "" {
- return s.log.String(), nil
- }
- s.appendLogLineLocked(msg)
- return s.log.String(), nil
-}
-
-func (s *progressSink) appendLogLineLocked(msg string) {
- if s.log.Len() > 0 {
- s.log.WriteByte('\n')
- }
- now := time.Now
- if s.now != nil {
- now = s.now
- }
- s.log.WriteString(now().Format("15:04:05"))
- s.log.WriteString(": ")
- s.log.WriteString(msg)
- log := trimLogHead(s.log.String(), progressLogMaxChars)
- if len(log) != s.log.Len() {
- s.log.Reset()
- s.log.WriteString(log)
- }
-}
-
-// trimLogHead drops whole lines from the head of text until the remainder
-// fits maxChars, keeping the newest lines. Text without a fitting newline
-// boundary is returned unchanged.
-func trimLogHead(text string, maxChars int) string {
- if len(text) <= maxChars {
- return text
- }
- for i := 0; i < len(text); i++ {
- if text[i] == '\n' && len(text)-(i+1) <= maxChars {
- return text[i+1:]
- }
+ if err := s.taskSvc.RecordMessage(ctx, s.pipelineLogID, taskID, fmt.Sprintf("%s: %s", component, message)); err != nil {
+ common.Error(fmt.Sprintf("progressSink: record message for task %s failed", taskID), err)
}
- return text
}
// deriveDocumentProgress computes the document-level progress (0..1) from the
diff --git a/internal/ingestion/service/progress_sink_test.go b/internal/ingestion/service/progress_sink_test.go
index ba6bd3d780..7680b53f65 100644
--- a/internal/ingestion/service/progress_sink_test.go
+++ b/internal/ingestion/service/progress_sink_test.go
@@ -18,13 +18,9 @@ package service
import (
"context"
- "errors"
- "fmt"
"runtime"
- "strings"
"sync"
"testing"
- "time"
"ragflow/internal/dao"
"ragflow/internal/ingestion/pipeline"
@@ -61,7 +57,7 @@ func TestProgressSink_EagerlyConstructsDocumentService(t *testing.T) {
defer cleanup()
ctx := t.Context()
- sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService())
+ sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService(), "run-1")
if sink.docSvc == nil {
t.Fatal("expected sink to eagerly construct its DocumentService, got nil (lazy)")
}
@@ -85,7 +81,7 @@ func TestProgressSink_DocService_NoDataRace(t *testing.T) {
// Deliberately do NOT inject a stub docSvc: the sink's own DocumentService
// must already be constructed (not lazily built mid-call) when the
// goroutines below race into docSvc.
- sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService())
+ sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService(), "run-1")
const n = 30
var wg sync.WaitGroup
@@ -115,7 +111,7 @@ func TestProgressSink_Total_NoDataRace(t *testing.T) {
_, _, _, taskID := testutil.SeedTestData(t, db, testutil.WithPipelineID("flow-1"))
ctx := t.Context()
- sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService())
+ sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService(), "run-1")
const n = 30
var wg sync.WaitGroup
@@ -142,62 +138,15 @@ func TestProgressSink_Total_NoDataRace(t *testing.T) {
}
type stubDocProgressSvc struct {
- gotDocID string
- gotProgress float64
- gotMsg string
- calls int
- doc *document.DocumentResponse
- docErr error
+ stateCalls int
+ stateDocID string
+ stateProgress float64
}
-func setProgressSinkTestClock(sink *progressSink) {
- sink.now = func() time.Time {
- return time.Date(2026, 8, 17, 3, 4, 5, 0, time.UTC)
- }
-}
-
-type blockingDocProgressSvc struct {
- mu sync.Mutex
- firstStarted chan struct{}
- releaseFirst chan struct{}
- messages []string
- updates int
-}
-
-func (s *blockingDocProgressSvc) GetDocumentByID(context.Context, string) (*document.DocumentResponse, error) {
- return nil, nil
-}
-
-func (s *blockingDocProgressSvc) UpdateRunState(context.Context, string, float64) error {
- return nil
-}
-
-func (s *blockingDocProgressSvc) UpdateRunProgress(_ context.Context, _ string, _ float64, msg string) error {
- s.mu.Lock()
- s.updates++
- update := s.updates
- s.messages = append(s.messages, msg)
- s.mu.Unlock()
- if update == 1 {
- close(s.firstStarted)
- <-s.releaseFirst
- }
- return nil
-}
-
-func (s *stubDocProgressSvc) GetDocumentByID(ctx context.Context, docID string) (*document.DocumentResponse, error) {
- return s.doc, s.docErr
-}
-
-func (s *stubDocProgressSvc) UpdateRunState(context.Context, string, float64) error {
- return nil
-}
-
-func (s *stubDocProgressSvc) UpdateRunProgress(ctx context.Context, docID string, progress float64, progressMsg string) error {
- s.calls++
- s.gotDocID = docID
- s.gotProgress = progress
- s.gotMsg = progressMsg
+func (s *stubDocProgressSvc) UpdateRunState(_ context.Context, docID string, progress float64) error {
+ s.stateCalls++
+ s.stateDocID = docID
+ s.stateProgress = progress
return nil
}
@@ -211,8 +160,7 @@ func TestProgressSinkPersistsViaService(t *testing.T) {
_, _, docID, taskID := testutil.SeedTestData(t, db, testutil.WithPipelineID("flow-1"))
ctx := t.Context()
- sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService())
- setProgressSinkTestClock(sink)
+ sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService(), "run-1")
stub := &stubDocProgressSvc{}
sink.docSvc = stub
@@ -233,7 +181,7 @@ func TestProgressSinkPersistsViaService(t *testing.T) {
Message: "Parser Done",
})
- logs, err := dao.NewIngestionTaskLogDAO().ListLogsByTaskID(ctx, db, taskID)
+ logs, err := dao.NewIngestionTaskLogDAO().ListLogsByPipelineLogID(ctx, db, "run-1")
if err != nil {
t.Fatalf("list logs: %v", err)
}
@@ -244,220 +192,40 @@ func TestProgressSinkPersistsViaService(t *testing.T) {
t.Fatalf("unexpected log row: %+v", logs[0])
}
- // 1 of 2 components done -> RUNNING (run "1"), progress 0.5.
- if stub.calls != 1 {
- t.Fatalf("UpdateRunProgress calls = %d, want 1", stub.calls)
- }
- if stub.gotDocID != docID {
- t.Fatalf("docID = %q, want %q", stub.gotDocID, docID)
- }
- if stub.gotProgress != 0.5 {
- t.Fatalf("progress = %v, want 0.5", stub.gotProgress)
- }
- if stub.gotMsg != "03:04:05: Parser Done" {
- t.Fatalf("progress_msg = %q, want timestamped Parser Done", stub.gotMsg)
- }
-}
-
-// TestProgressSinkAccumulatesProgressLog pins the core fix: document.progress_msg
-// is the accumulated multi-line run log, not just the latest component line.
-// The document-details dialog renders progress_msg verbatim (whitespace-pre-line).
-func TestProgressSinkAccumulatesProgressLog(t *testing.T) {
- db := testutil.SetupTestDB(t)
- cleanup := testutil.ReplaceDBForTest(t, db)
- defer cleanup()
- _, _, docID, taskID := testutil.SeedTestData(t, db, testutil.WithPipelineID("flow-1"))
-
- ctx := t.Context()
- sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService())
- setProgressSinkTestClock(sink)
- stub := &stubDocProgressSvc{}
- sink.docSvc = stub
- sink.OnComponentTotal(ctx, taskID, 2)
-
- sink.OnComponentProgress(ctx, pipeline.ProgressEvent{
- TaskID: taskID, DocumentID: docID, Component: "File", Phase: 1, Message: "File:naive Done",
- })
- sink.OnComponentProgress(ctx, pipeline.ProgressEvent{
- TaskID: taskID, DocumentID: docID, Component: "Parser", Phase: 1, Message: "Parser Done",
- })
-
- want := "03:04:05: File:naive Done\n03:04:05: Parser Done"
- if stub.gotMsg != want {
- t.Fatalf("progress_msg = %q, want multi-line log %q", stub.gotMsg, want)
- }
-}
-
-// TestProgressSinkSeedsLogFromDocument verifies the accumulated log keeps the
-// lines already stored on the document row when a run resumes.
-func TestProgressSinkSeedsLogFromDocument(t *testing.T) {
- db := testutil.SetupTestDB(t)
- cleanup := testutil.ReplaceDBForTest(t, db)
- defer cleanup()
- _, _, docID, taskID := testutil.SeedTestData(t, db, testutil.WithPipelineID("flow-1"))
-
- ctx := t.Context()
- sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService())
- setProgressSinkTestClock(sink)
- seed := "File:naive Done"
- stub := &stubDocProgressSvc{doc: &document.DocumentResponse{ProgressMsg: &seed}}
- sink.docSvc = stub
- sink.OnComponentTotal(ctx, taskID, 2)
-
- sink.OnComponentProgress(ctx, pipeline.ProgressEvent{
- TaskID: taskID, DocumentID: docID, Component: "Parser", Phase: 1, Message: "Parser Done",
- })
-
- want := "File:naive Done\n03:04:05: Parser Done"
- if stub.gotMsg != want {
- t.Fatalf("progress_msg = %q, want seeded multi-line log %q", stub.gotMsg, want)
- }
-}
-
-func TestProgressSinkRetriesSeedAfterReadFailure(t *testing.T) {
- seed := "existing"
- stub := &stubDocProgressSvc{docErr: errors.New("temporary read failure")}
- sink := &progressSink{docSvc: stub}
-
- if got := sink.accumulateLog(context.Background(), "doc-1", "pending"); got != "" {
- t.Fatalf("log after failed seed = %q, want empty mirror value", got)
- }
-
- stub.docErr = nil
- stub.doc = &document.DocumentResponse{ProgressMsg: &seed}
- setProgressSinkTestClock(sink)
- got := sink.accumulateLog(context.Background(), "doc-1", "current")
- if want := "existing\n03:04:05: pending\n03:04:05: current"; got != want {
- t.Fatalf("log after seed retry = %q, want %q", got, want)
- }
-}
-
-func TestProgressSinkSerializesLogAndDocumentWrite(t *testing.T) {
- stub := &blockingDocProgressSvc{
- firstStarted: make(chan struct{}),
- releaseFirst: make(chan struct{}),
- }
- sink := &progressSink{docSvc: stub}
- setProgressSinkTestClock(sink)
-
- firstDone := make(chan struct{})
- go func() {
- defer close(firstDone)
- if err := sink.updateDocumentProgress(context.Background(), "doc-1", 0.1, "first"); err != nil {
- t.Errorf("first update: %v", err)
- }
- }()
- <-stub.firstStarted
-
- secondDone := make(chan struct{})
- go func() {
- defer close(secondDone)
- if err := sink.updateDocumentProgress(context.Background(), "doc-1", 0.2, "second"); err != nil {
- t.Errorf("second update: %v", err)
- }
- }()
-
- select {
- case <-secondDone:
- t.Fatal("second update completed before the first document write was released")
- default:
- }
- close(stub.releaseFirst)
- <-firstDone
- <-secondDone
-
- stub.mu.Lock()
- defer stub.mu.Unlock()
- if len(stub.messages) != 2 || stub.messages[1] != "03:04:05: first\n03:04:05: second" {
- t.Fatalf("document writes = %q, want accumulated second write", stub.messages)
+ // 1 of 2 components done -> RUNNING (run "1"), progress 0.5. The
+ // event stream owns text, so this must not write document.progress_msg.
+ if stub.stateCalls != 1 || stub.stateDocID != docID || stub.stateProgress != 0.5 {
+ t.Fatalf("UpdateRunState = calls:%d doc:%q progress:%v, want 1/%q/0.5", stub.stateCalls, stub.stateDocID, stub.stateProgress, docID)
}
}
-// TestProgressSinkTrimsLogHead verifies the accumulated log is head-trimmed at
-// line boundaries once it exceeds progressLogMaxChars, keeping the newest lines
-// while keeping the newest complete lines.
-func TestProgressSinkTrimsLogHead(t *testing.T) {
+func TestProgressSinkWritesLifecycleEventForCapturedRun(t *testing.T) {
db := testutil.SetupTestDB(t)
cleanup := testutil.ReplaceDBForTest(t, db)
defer cleanup()
- _, _, docID, taskID := testutil.SeedTestData(t, db, testutil.WithPipelineID("flow-1"))
-
- ctx := t.Context()
- sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService())
- setProgressSinkTestClock(sink)
- stub := &stubDocProgressSvc{}
- sink.docSvc = stub
- sink.OnComponentTotal(ctx, taskID, 2)
+ _, _, _, taskID := testutil.SeedTestData(t, db, testutil.WithPipelineID("flow-1"))
- head := strings.Repeat("h", 2000)
- tail := strings.Repeat("t", 2000)
- sink.OnComponentProgress(ctx, pipeline.ProgressEvent{
- TaskID: taskID, DocumentID: docID, Component: "File", Phase: 1, Message: head,
- })
- sink.OnComponentProgress(ctx, pipeline.ProgressEvent{
- TaskID: taskID, DocumentID: docID, Component: "Parser", Phase: 1, Message: tail,
+ sink := newProgressSink(t.Context(), servicepkg.NewIngestionTaskService(), "run-1")
+ sink.OnComponentProgress(t.Context(), pipeline.ProgressEvent{
+ TaskID: taskID,
+ Component: "Parser",
+ Phase: 1,
+ Message: "Parser Done",
})
- if len(stub.gotMsg) > progressLogMaxChars {
- t.Fatalf("progress_msg len = %d, exceeds %d", len(stub.gotMsg), progressLogMaxChars)
- }
- if stub.gotMsg != "03:04:05: "+tail {
- t.Fatalf("progress_msg keeps the newest line: got prefix %q, want %q", stub.gotMsg[:20], tail[:20])
- }
-}
-
-// TestProgressSink_Log_NoDataRace hits the accumulated log buffer directly:
-// OnComponentProgress fires from concurrent parallel-branch goroutines, so the
-// lazy seed and the append must be mutex-guarded. The direct call avoids the
-// test-DB serialization inside OnComponentProgress that would mask the race.
-func TestProgressSink_Log_NoDataRace(t *testing.T) {
- db := testutil.SetupTestDB(t)
- cleanup := testutil.ReplaceDBForTest(t, db)
- defer cleanup()
-
- ctx := t.Context()
- sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService())
- sink.docSvc = &stubDocProgressSvc{}
-
- const n = 30
- var wg sync.WaitGroup
- start := make(chan struct{})
- for i := 0; i < n; i++ {
- wg.Add(1)
- go func(i int) {
- defer wg.Done()
- <-start
- sink.accumulateLog(ctx, "doc-1", fmt.Sprintf("line-%d", i))
- }(i)
+ logs, err := dao.NewIngestionTaskLogDAO().ListLogsByPipelineLogID(t.Context(), db, "run-1")
+ if err != nil {
+ t.Fatalf("list events: %v", err)
}
- close(start)
- wg.Wait()
-
- got := sink.accumulateLog(ctx, "doc-1", "")
- if lines := strings.Split(got, "\n"); len(lines) != n {
- t.Fatalf("accumulated log lines = %d, want %d: %q", len(lines), n, got)
+ if len(logs) != 1 {
+ t.Fatalf("event count = %d, want 1", len(logs))
}
-}
-
-func TestTrimLogHead(t *testing.T) {
- tests := []struct {
- name string
- text string
- max int
- want string
- }{
- {name: "short text unchanged", text: "a\nb", max: 10, want: "a\nb"},
- {name: "exactly max unchanged", text: "a\nb", max: 3, want: "a\nb"},
- {name: "drops head line", text: "l1\nl2\nl3", max: 6, want: "l2\nl3"},
- {name: "keeps suffix at exact limit", text: "l1\nl2\nl3", max: 5, want: "l2\nl3"},
- {name: "no fitting newline unchanged", text: "abcdef", max: 3, want: "abcdef"},
+ event := logs[0]
+ if event.PipelineLogID == nil || *event.PipelineLogID != "run-1" {
+ t.Fatalf("pipeline_log_id = %v, want run-1", event.PipelineLogID)
}
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if got := trimLogHead(tt.text, tt.max); got != tt.want {
- t.Errorf("trimLogHead(%q, %d) = %q, want %q", tt.text, tt.max, got, tt.want)
- }
- })
+ if event.EventType != dao.EventTypeLifecycle || event.Component != "Parser" || event.Phase != 1 {
+ t.Fatalf("event = %+v, want lifecycle Parser/1", event)
}
}
@@ -470,7 +238,7 @@ func TestProgressSinkEmptyDocumentIDSkipsMirror(t *testing.T) {
_, _, _, taskID := testutil.SeedTestData(t, db, testutil.WithPipelineID("flow-1"))
ctx := t.Context()
- sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService())
+ sink := newProgressSink(ctx, servicepkg.NewIngestionTaskService(), "run-1")
stub := &stubDocProgressSvc{}
sink.docSvc = stub
@@ -481,15 +249,15 @@ func TestProgressSinkEmptyDocumentIDSkipsMirror(t *testing.T) {
Message: "Chunker Done",
})
- logs, err := dao.NewIngestionTaskLogDAO().ListLogsByTaskID(ctx, db, taskID)
+ logs, err := dao.NewIngestionTaskLogDAO().ListLogsByPipelineLogID(ctx, db, "run-1")
if err != nil {
t.Fatalf("list logs: %v", err)
}
if len(logs) != 1 {
t.Fatalf("expected 1 component-progress row, got %d", len(logs))
}
- if stub.calls != 0 {
- t.Fatalf("UpdateRunProgress calls = %d, want 0 (no document bound)", stub.calls)
+ if stub.stateCalls != 0 {
+ t.Fatalf("UpdateRunState calls = %d, want 0 (no document bound)", stub.stateCalls)
}
}
diff --git a/internal/ingestion/service/run_task_test.go b/internal/ingestion/service/run_task_test.go
index 2e6ab2dfac..3307817a3f 100644
--- a/internal/ingestion/service/run_task_test.go
+++ b/internal/ingestion/service/run_task_test.go
@@ -11,15 +11,14 @@ import (
"ragflow/internal/ingestion/testutil"
)
-// TestRunTask_ContextCancelledBeforeCheckpoint: a cancelled context makes
-// runTask return true (terminal: durably recorded cancel) immediately, without
-// bumping the checkpoint or calling runDocumentTask. The task status is
-// transitioned to STOPPED so it does not stay RUNNING forever.
+// TestRunTask_ContextCancelledBeforePipeline makes a cancelled context settle
+// the task as STOPPED without entering the pipeline.
func TestRunTask_ContextCancelledBeforeCheckpoint(t *testing.T) {
db := testutil.SetupTestDB(t)
cleanup := testutil.ReplaceDBForTest(t, db)
defer cleanup()
_, _, _, taskID := testutil.SeedTestData(t, db, testutil.WithPipelineID("flow-1"))
+ runID := "run-" + taskID
ingestor := newUnitIngestor("test", 1, []string{"pdf"})
var runDocCalled bool
@@ -32,7 +31,7 @@ func TestRunTask_ContextCancelledBeforeCheckpoint(t *testing.T) {
cancel()
terminal := ingestor.runTask(ctx, &entity.IngestionTask{
- ID: taskID, DocumentID: "doc-1", DatasetID: "kb-1",
+ ID: taskID, DocumentID: "doc-1", DatasetID: "kb-1", PipelineLogID: &runID,
})
if !terminal {
@@ -42,13 +41,14 @@ func TestRunTask_ContextCancelledBeforeCheckpoint(t *testing.T) {
t.Fatal("expected runDocumentTask to be skipped on cancelled ctx")
}
testCtx := t.Context()
- // Checkpoint must not have been bumped — no log row should exist.
- logs, err := dao.NewIngestionTaskLogDAO().ListLogsByTaskID(testCtx, db, taskID)
+ // Cancellation is terminal for the bound run even when it happens before
+ // pipeline execution begins.
+ logs, err := dao.NewIngestionTaskLogDAO().ListLogsByPipelineLogID(testCtx, db, "run-"+taskID)
if err != nil {
t.Fatalf("list logs: %v", err)
}
- if len(logs) != 0 {
- t.Fatalf("expected 0 checkpoint rows (ctx cancelled before checkpoint), got %d", len(logs))
+ if len(logs) != 1 || logs[0].EventType != dao.EventTypeTerminal || logs[0].Message != "Task stopped by user." {
+ t.Fatalf("terminal events = %+v, want one stopped event", logs)
}
// Task must be STOPPED, not left in RUNNING.
task, err := dao.NewIngestionTaskDAO().GetByID(testCtx, db, taskID)
@@ -60,53 +60,6 @@ func TestRunTask_ContextCancelledBeforeCheckpoint(t *testing.T) {
}
}
-// TestRunTask_CheckpointFailureMarksFailed: a corrupted run_count value is
-// skipped by IncrementRunCount (it scans past unparseable rows). The task
-// proceeds normally and completes.
-func TestRunTask_CorruptedRunCountSkipped(t *testing.T) {
- db := testutil.SetupTestDB(t)
- cleanup := testutil.ReplaceDBForTest(t, db)
- defer cleanup()
- _, _, docID, taskID := testutil.SeedTestData(t, db, testutil.WithPipelineID("flow-1"))
-
- // Seed a bad checkpoint: run_count is a string, not a number.
- if err := db.Create(&entity.IngestionTaskLog{
- TaskID: taskID,
- Checkpoint: entity.JSONMap{
- "run_count": "not-a-number",
- },
- }).Error; err != nil {
- t.Fatalf("insert bad log: %v", err)
- }
-
- ingestor := newUnitIngestor("test", 1, []string{"pdf"})
- var runDocCalled bool
- ingestor.runDocumentTask = func(ctx context.Context, _ *entity.IngestionTask) error {
- runDocCalled = true
- return nil
- }
-
- terminal := ingestor.runTask(t.Context(), &entity.IngestionTask{
- ID: taskID, DocumentID: docID, DatasetID: "kb-1", Status: common.RUNNING,
- })
-
- if !terminal {
- t.Fatal("expected true (terminal: completed despite corrupted run_count)")
- }
- if !runDocCalled {
- t.Fatal("expected runDocumentTask to be called (bad run_count is skipped, not fatal)")
- }
-
- ctx := t.Context()
- task, err := dao.NewIngestionTaskDAO().GetByID(ctx, db, taskID)
- if err != nil {
- t.Fatalf("load task: %v", err)
- }
- if task.Status != common.COMPLETED {
- t.Fatalf("task status = %s, want COMPLETED", task.Status)
- }
-}
-
// TestRunTask_RunDocumentTaskFailureMarksFailed: when runDocumentTask errors,
// runTask marks the task FAILED and returns durably-written.
func TestRunTask_RunDocumentTaskFailureMarksFailed(t *testing.T) {
@@ -120,8 +73,10 @@ func TestRunTask_RunDocumentTaskFailureMarksFailed(t *testing.T) {
return errors.New("boom")
}
+ runID := "run-" + taskID
terminal := ingestor.runTask(t.Context(), &entity.IngestionTask{
ID: taskID, DocumentID: docID, DatasetID: "kb-1", Status: common.RUNNING,
+ PipelineLogID: &runID,
})
if !terminal {
@@ -136,6 +91,13 @@ func TestRunTask_RunDocumentTaskFailureMarksFailed(t *testing.T) {
if task.Status != common.FAILED {
t.Fatalf("task status = %s, want FAILED", task.Status)
}
+ logs, err := dao.NewIngestionTaskLogDAO().ListLogsByPipelineLogID(ctx, db, runID)
+ if err != nil {
+ t.Fatalf("list terminal events: %v", err)
+ }
+ if len(logs) != 1 || logs[0].EventType != dao.EventTypeTerminal || logs[0].Message == "" {
+ t.Fatalf("terminal events = %+v, want one terminal event", logs)
+ }
}
// TestRunTask_PipelineCancelledMarksStopped: when runDocumentTask returns
diff --git a/internal/ingestion/service/test_helpers.go b/internal/ingestion/service/test_helpers.go
index 05e887d1ce..008f862399 100644
--- a/internal/ingestion/service/test_helpers.go
+++ b/internal/ingestion/service/test_helpers.go
@@ -16,8 +16,6 @@
package service
-import "context"
-
// ──────────────────────────────────────────────────────────
// Ingestor Test Helpers
// ──────────────────────────────────────────────────────────
@@ -34,9 +32,6 @@ type IngestorOption func(*Ingestor)
func newUnitIngestor(name string, maxConcurrency int32, supportedTypes []string) *Ingestor {
ingestor := NewIngestor(name, maxConcurrency, supportedTypes)
- ingestor.checkpointExists = func(context.Context, string) (bool, error) {
- return false, nil
- }
return ingestor
}
diff --git a/internal/ingestion/service/worker_dispatcher_helpers_test.go b/internal/ingestion/service/worker_dispatcher_helpers_test.go
index 65026fafdb..1367aa465e 100644
--- a/internal/ingestion/service/worker_dispatcher_helpers_test.go
+++ b/internal/ingestion/service/worker_dispatcher_helpers_test.go
@@ -50,8 +50,22 @@ func seedBurstTasks(t *testing.T, db *gorm.DB, n int) []string {
}).Error; err != nil {
t.Fatalf("create document %s: %v", docID, err)
}
+ runCount := 1
+ runID := "burst-run-" + fmt.Sprint(i)
+ if err := db.Create(&entity.PipelineOperationLog{
+ ID: runID,
+ DocumentID: docID,
+ RunCount: &runCount,
+ TenantID: tenantID,
+ KbID: kbID,
+ ParserID: "naive",
+ TaskType: string(entity.PipelineTaskTypeParse),
+ OperationStatus: string(entity.TaskStatusUnstart),
+ }).Error; err != nil {
+ t.Fatalf("create pipeline log %s: %v", runID, err)
+ }
if err := db.Create(&entity.IngestionTask{
- ID: taskID, UserID: "u1", DocumentID: docID, DatasetID: kbID, Status: common.CREATED,
+ ID: taskID, UserID: "u1", DocumentID: docID, DatasetID: kbID, Status: common.CREATED, PipelineLogID: &runID,
}).Error; err != nil {
t.Fatalf("create ingestion task %s: %v", taskID, err)
}
diff --git a/internal/ingestion/task/debug_test.go b/internal/ingestion/task/debug_test.go
index 0f5fc97654..34f08a4e77 100644
--- a/internal/ingestion/task/debug_test.go
+++ b/internal/ingestion/task/debug_test.go
@@ -28,9 +28,7 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
- "gorm.io/gorm"
"ragflow/internal/common"
- "ragflow/internal/entity"
"ragflow/internal/ingestion/component"
"ragflow/internal/ingestion/component/globals"
"ragflow/internal/ingestion/knowledge_compile"
@@ -75,7 +73,6 @@ func TestNewDebugTaskContext_InjectsDebugID(t *testing.T) {
func TestExecute_DebugViaEntry(t *testing.T) {
taskCtx := NewDebugTaskContext("t1", "canvas-1", "doc.pdf", []byte("page one\fpage two\fpage three"))
- logCalled := false
insertCalled := false
exec, err := NewPipelineExecutor(taskCtx, "canvas-1", 0)
@@ -96,10 +93,6 @@ func TestExecute_DebugViaEntry(t *testing.T) {
},
}, dsl, nil
}).
- WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- logCalled = true
- return nil
- }).
WithInsertFunc(func(ctx context.Context, chunks []map[string]any, baseName, datasetID string) ([]string, error) {
insertCalled = true
return nil, nil
@@ -115,9 +108,6 @@ func TestExecute_DebugViaEntry(t *testing.T) {
if len(result.Chunks) != 4 {
t.Errorf("len(result.Chunks) = %d, want 4", len(result.Chunks))
}
- if logCalled {
- t.Error("pipeline log should NOT be created in a debug (kb_id == \"\") run")
- }
if insertCalled {
t.Error("chunk insert should NOT be called in a debug (kb_id == \"\") run")
}
diff --git a/internal/ingestion/task/knowledge_compiler_wiring.go b/internal/ingestion/task/knowledge_compiler_wiring.go
index 3fa1af214e..92d682d08b 100644
--- a/internal/ingestion/task/knowledge_compiler_wiring.go
+++ b/internal/ingestion/task/knowledge_compiler_wiring.go
@@ -604,6 +604,7 @@ func (c *kcChatInvoker) Chat(ctx context.Context, req kc.ChatRequest) (*kc.ChatR
// is never cached, so each attempt issues a fresh request. Permanent
// configuration/model errors (auth, unknown model) are not retried.
var resp *models.ChatResponse
+ failureReporter := kc.RetryFailureReporter{}
attempt := 0
call := func() error {
attempt++
@@ -615,15 +616,23 @@ func (c *kcChatInvoker) Chat(ctx context.Context, req kc.ChatRequest) (*kc.ChatR
r, err := c.svc.Chat(attemptCtx, c.tenantID, llmID, msgs, config)
if err != nil {
if !req.DisableRetry {
- message := fmt.Sprintf("[ERROR] LLM call failed (attempt %d/%d): %s", attempt, kcChatRetryMax+1, kc.CompactError(err))
- if appcommon.IsTransientError(err) && attempt <= kcChatRetryMax {
- message += "; retrying with exponential backoff"
+ final := !appcommon.IsTransientError(err) || attempt > kcChatRetryMax
+ delay := kcChatRetryDelay
+ if final {
+ delay = 0
+ }
+ if message, ok := failureReporter.FailureMessage(attempt, kcChatRetryMax+1, delay, err, final); ok {
+ runtime.ReportProgressMessage(ctx, "Compiler", message)
}
- runtime.ReportProgressMessage(ctx, "Compiler", message)
}
return err
}
resp = r
+ if !req.DisableRetry {
+ if message, ok := failureReporter.RecoveryMessage(attempt); ok {
+ runtime.ReportProgressMessage(ctx, "Compiler", message)
+ }
+ }
return nil
}
if req.DisableRetry {
diff --git a/internal/ingestion/task/pipeline_executor.go b/internal/ingestion/task/pipeline_executor.go
index 170a53bef1..310387520b 100644
--- a/internal/ingestion/task/pipeline_executor.go
+++ b/internal/ingestion/task/pipeline_executor.go
@@ -21,7 +21,6 @@ import (
"encoding/json"
"errors"
"fmt"
- "ragflow/internal/utility"
"sort"
"strings"
"time"
@@ -71,7 +70,6 @@ type PipelineExecutor struct {
docBulkSize int
indexWriter *chunkIndexWriter
- logCreateFunc func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error
loadDSLFunc func(ctx context.Context, canvasID string) (string, string, error)
runPipelineFunc func(ctx context.Context, dsl string) (map[string]any, string, error)
progressSink pipelinepkg.ProgressSink
@@ -122,7 +120,6 @@ func NewPipelineExecutor(
taskCtx.Doc.KbID,
docBulkSize,
),
- logCreateFunc: dao.NewPipelineOperationLogDAO().Create,
}
svc.loadDSLFunc = svc.loadDSLFromCanvas
svc.runPipelineFunc = svc.runPipelineWithDSL
@@ -134,11 +131,6 @@ func (s *PipelineExecutor) WithInsertFunc(f InsertFunc) *PipelineExecutor {
return s
}
-func (s *PipelineExecutor) WithLogCreateFunc(f func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error) *PipelineExecutor {
- s.logCreateFunc = f
- return s
-}
-
func (s *PipelineExecutor) WithLoadDSLFunc(f func(ctx context.Context, canvasID string) (string, string, error)) *PipelineExecutor {
s.loadDSLFunc = f
return s
@@ -199,7 +191,7 @@ func (s *PipelineExecutor) Execute(ctx context.Context) (*PipelineResult, error)
return nil, err
}
- if pipelineDSL != "" {
+ if pipelineDSL != "" && s.taskCtx.IngestionTask != nil && s.taskCtx.IngestionTask.PipelineLogID != nil && *s.taskCtx.IngestionTask.PipelineLogID != "" {
s.recordPipelineLog(context.WithoutCancel(ctx), dao.DB, s.taskCtx.Doc.ID, pipelineDSL, string(entity.TaskStatusDone))
}
@@ -770,30 +762,40 @@ type PipelineLogInput struct {
Status string
Document entity.Document
// PipelineLogID is the id of the pipeline_operation_log row this run owns
- // (ingestion_task.pipeline_log_id). When set, the terminal write updates
- // exactly that row and never creates a second one — so a superseded run
- // whose row was deleted cannot adopt the replacement run's row. Empty for
- // legacy, debug-adjacent, or non-ingestion callers.
+ // (ingestion_task.pipeline_log_id). The public writer requires it and updates
+ // exactly that row, never creating a second one — so a superseded run whose
+ // row was deleted cannot adopt the replacement run's row.
PipelineLogID string
}
+// ErrMissingRunIdentity reports an attempt to persist an ingestion terminal
+// outcome without the run row that owns it.
+var ErrMissingRunIdentity = errors.New("pipeline log id is required")
+
// RecordPipelineLog persists a pipeline operation log without requiring
// executor setup. Callers should pass the status in Status.
//
// When the run created a queued row (CREATED/SCHEDULED), the terminal write
// advances that same row so the dataset detail page shows one entry per run.
-// Without a bound row (legacy runs, debug-adjacent paths) it adopts the
-// document's open row, or creates a new row when none exists.
+// A missing run identity is rejected before any database lookup or fallback.
func RecordPipelineLog(ctx context.Context, db *gorm.DB, input PipelineLogInput) error {
- return recordPipelineLog(ctx, db, input, dao.NewPipelineOperationLogDAO().Create)
+ if strings.TrimSpace(input.PipelineLogID) == "" {
+ return ErrMissingRunIdentity
+ }
+ return recordPipelineLog(ctx, db, input)
}
func recordPipelineLog(
ctx context.Context,
db *gorm.DB,
input PipelineLogInput,
- createFunc func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error,
) error {
+ if strings.TrimSpace(input.PipelineLogID) == "" {
+ return ErrMissingRunIdentity
+ }
+ if db == nil {
+ return errors.New("pipeline log database is required")
+ }
var dslMap entity.JSONMap
if strings.TrimSpace(input.DSL) == "" {
dslMap = entity.JSONMap{}
@@ -861,80 +863,24 @@ func recordPipelineLog(
if doc.Status != nil && *doc.Status != "" {
statusValue = *doc.Status
}
- sourceFrom := doc.SourceType
- if parts := strings.SplitN(sourceFrom, "/", 2); len(parts) > 0 {
- sourceFrom = parts[0]
- }
- documentName := ""
- if doc.Name != nil {
- documentName = *doc.Name
+ if err := updateOpenLogRow(ctx, db, input, operationStatus, statusValue, pipelineID, pipelineTitle, pipelineAvatar, dslMap, doc); err != nil {
+ return fmt.Errorf("advance pipeline log for document %s: %w", input.DocumentID, err)
}
- if db != nil {
- if handled, err := updateOpenLogRow(ctx, db, input, operationStatus, statusValue, pipelineID, pipelineTitle, pipelineAvatar, dslMap, doc); err != nil {
- common.Warn(fmt.Sprintf("failed to advance open pipeline log for document %s: %v", input.DocumentID, err))
- } else if handled {
- return nil
- }
- }
- log := &entity.PipelineOperationLog{
- ID: utility.GenerateUUID(),
- TenantID: input.TenantID,
- KbID: input.KbID,
- DocumentID: input.DocumentID,
- PipelineID: pipelineID,
- PipelineTitle: &pipelineTitle,
- TaskType: string(entity.PipelineTaskTypeParse),
- DSL: dslMap,
- ParserID: doc.ParserID,
- DocumentName: documentName,
- DocumentSuffix: doc.Suffix,
- DocumentType: doc.Type,
- SourceFrom: sourceFrom,
- Progress: doc.Progress,
- ProgressMsg: doc.ProgressMsg,
- ProcessBeginAt: doc.ProcessBeginAt,
- ProcessDuration: doc.ProcessDuration,
- OperationStatus: operationStatus,
- Avatar: pipelineAvatar,
- Status: &statusValue,
- }
- return createFunc(ctx, db, log)
+ return nil
}
// updateOpenLogRow advances the row this run already owns to its terminal
// state, filling in the DSL, the pipeline identity, and the final progress
-// snapshot. It reports whether the caller must stop (true) or insert a new row
-// (false).
+// snapshot.
//
-// The row is targeted by PipelineLogID when the caller has one, so a superseded run
-// whose row was deleted with the task can never reach into the replacement
-// run's row. Only when the caller has no bound row (legacy, debug-adjacent, or
-// non-ingestion callers) does it adopt the document's newest unowned open row,
-// so a stray open row is closed rather than left queued; a row a live run owns
-// is never adopted, because that run's own terminal write would then be lost.
+// The row is targeted by PipelineLogID, so a superseded run whose row was
+// deleted with the task can never reach into the replacement run's row.
// RowsAffected is not used to decide whether to create: once a target row is
// known, the write is final — a lost CAS means another writer already finalized
// this run or the row was dropped with a superseded run, and neither may
// produce a second entry.
-func updateOpenLogRow(ctx context.Context, db *gorm.DB, input PipelineLogInput, operationStatus, statusValue string, pipelineID *string, pipelineTitle string, pipelineAvatar *string, dslMap entity.JSONMap, doc entity.Document) (bool, error) {
+func updateOpenLogRow(ctx context.Context, db *gorm.DB, input PipelineLogInput, operationStatus, statusValue string, pipelineID *string, pipelineTitle string, pipelineAvatar *string, dslMap entity.JSONMap, doc entity.Document) error {
targetID := input.PipelineLogID
- if targetID == "" {
- open, err := dao.NewPipelineOperationLogDAO().GetOpenLogByDocumentID(ctx, db, input.DocumentID)
- if err != nil {
- return false, err
- }
- if open == nil {
- return false, nil
- }
- owned, err := dao.NewPipelineOperationLogDAO().HasLiveTaskOwner(ctx, db, open.ID)
- if err != nil {
- return false, err
- }
- if owned {
- return false, nil
- }
- targetID = open.ID
- }
updates := map[string]interface{}{
"operation_status": operationStatus,
"status": statusValue,
@@ -943,7 +889,6 @@ func updateOpenLogRow(ctx context.Context, db *gorm.DB, input PipelineLogInput,
"pipeline_title": pipelineTitle,
"avatar": pipelineAvatar,
"progress": doc.Progress,
- "progress_msg": doc.ProgressMsg,
"process_duration": doc.ProcessDuration,
"parser_id": doc.ParserID,
"source_from": strings.SplitN(doc.SourceType, "/", 2)[0],
@@ -963,7 +908,7 @@ func updateOpenLogRow(ctx context.Context, db *gorm.DB, input PipelineLogInput,
Where("id = ? AND operation_status IN ?", targetID, dao.OpenPipelineOperationStatuses()).
Updates(updates)
if result.Error != nil {
- return false, result.Error
+ return result.Error
}
// No second entry is created for a known target, so a row that is gone or
// already finalized means this run's outcome is not recorded anywhere. That
@@ -972,7 +917,7 @@ func updateOpenLogRow(ctx context.Context, db *gorm.DB, input PipelineLogInput,
if result.RowsAffected == 0 {
common.Warn(fmt.Sprintf("pipeline log %s for document %s is gone or already terminal; %s entry dropped", targetID, input.DocumentID, operationStatus))
}
- return true, nil
+ return nil
}
func (s *PipelineExecutor) recordPipelineLog(ctx context.Context, db *gorm.DB, docID, dsl, status string) {
@@ -993,7 +938,7 @@ func (s *PipelineExecutor) recordPipelineLog(ctx context.Context, db *gorm.DB, d
Status: status,
Document: s.taskCtx.Doc,
PipelineLogID: pipelineLogID,
- }, s.logCreateFunc); err != nil {
+ }); err != nil {
common.Warn(fmt.Sprintf("failed to record pipeline log: %v", err))
}
}
diff --git a/internal/ingestion/task/pipeline_executor_persist_test.go b/internal/ingestion/task/pipeline_executor_persist_test.go
index aeca8a05e4..156f66ed86 100644
--- a/internal/ingestion/task/pipeline_executor_persist_test.go
+++ b/internal/ingestion/task/pipeline_executor_persist_test.go
@@ -20,7 +20,6 @@ import (
"context"
"testing"
- "gorm.io/gorm"
"ragflow/internal/entity"
)
@@ -58,11 +57,6 @@ func TestExecute_DebugSkipsPipelineLog(t *testing.T) {
return map[string]any{"chunks": []map[string]any{}}, "dsl", nil
})
- var logCalled bool
- exec.WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- logCalled = true
- return nil
- })
var insertCalled bool
exec.WithInsertFunc(func(ctx context.Context, chunks []map[string]any, _, _ string) ([]string, error) {
insertCalled = true
@@ -72,9 +66,6 @@ func TestExecute_DebugSkipsPipelineLog(t *testing.T) {
if _, err := exec.Execute(context.Background()); err != nil {
t.Fatalf("Execute: %v", err)
}
- if logCalled {
- t.Errorf("recordPipelineLog must not be called in a debug (kb_id == \"\") run")
- }
if insertCalled {
t.Errorf("index insert must not be called in a debug (kb_id == \"\") run")
}
@@ -118,11 +109,6 @@ func TestExecute_DebugReturnsChunks(t *testing.T) {
}, "dsl", nil
})
- var logCalled bool
- exec.WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- logCalled = true
- return nil
- })
var insertCalled bool
exec.WithInsertFunc(func(ctx context.Context, chunks []map[string]any, _, _ string) ([]string, error) {
insertCalled = true
@@ -142,9 +128,6 @@ func TestExecute_DebugReturnsChunks(t *testing.T) {
if result.ChunkCount != 3 {
t.Errorf("expected ChunkCount 3, got %d", result.ChunkCount)
}
- if logCalled {
- t.Errorf("recordPipelineLog must not be called in a debug (kb_id == \"\") run")
- }
if insertCalled {
t.Errorf("index insert must not be called in a debug (kb_id == \"\") run")
}
diff --git a/internal/ingestion/task/pipeline_executor_test.go b/internal/ingestion/task/pipeline_executor_test.go
index bc26128345..a735d35e61 100644
--- a/internal/ingestion/task/pipeline_executor_test.go
+++ b/internal/ingestion/task/pipeline_executor_test.go
@@ -179,7 +179,7 @@ func TestNewPipelineExecutor_Basic(t *testing.T) {
if svc.taskCtx == nil {
t.Error("taskCtx should not be nil")
}
- if svc.indexWriter == nil || svc.logCreateFunc == nil || svc.loadDSLFunc == nil || svc.runPipelineFunc == nil {
+ if svc.indexWriter == nil || svc.loadDSLFunc == nil || svc.runPipelineFunc == nil {
t.Fatal("expected production dependencies to be fully initialized")
}
}
@@ -322,54 +322,18 @@ func TestInsertChunks_BaseNameAndDatasetID(t *testing.T) {
}
}
-func TestRecordPipelineLog(t *testing.T) {
- svc := mustNewPipelineExecutor(t, makeTaskCtx(), "flow-1", 0).WithLogCreateFunc(
- func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error { return nil },
- )
- ctx := t.Context()
- svc.recordPipelineLog(ctx, dao.DB, "doc-1", `{"components": {}}`, "done")
-}
-
-func TestRecordPipelineLog_InvalidJSONFallback(t *testing.T) {
- var captured *entity.PipelineOperationLog
- svc := mustNewPipelineExecutor(t, makeTaskCtx(), "flow-1", 0).WithLogCreateFunc(
- func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- captured = log
- return nil
- },
- )
- ctx := t.Context()
- svc.recordPipelineLog(ctx, dao.DB, "doc-1", "not-valid-json", "done")
- if captured == nil {
- t.Fatal("logCreateFunc was not called")
- }
- raw, ok := captured.DSL["raw"].(string)
- if !ok || raw != "not-valid-json" {
- t.Fatalf("DSL = %v, want {\"raw\": \"not-valid-json\"}", captured.DSL)
- }
-}
-
-func TestRecordPipelineLog_ValidJSONParsed(t *testing.T) {
- var captured *entity.PipelineOperationLog
- svc := mustNewPipelineExecutor(t, makeTaskCtx(), "flow-1", 0).WithLogCreateFunc(
- func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- captured = log
- return nil
- },
- )
- ctx := t.Context()
- svc.recordPipelineLog(ctx, dao.DB, "doc-1", `{"components": {"a": {"obj": {"component_name": "Parser", "params": {}}}}}`, "done")
- if captured == nil {
- t.Fatal("logCreateFunc was not called")
- }
- if captured.DSL["raw"] != nil {
- t.Fatalf("DSL should be parsed JSON, not fallback raw; got %v", captured.DSL)
- }
-}
-
func TestRecordPipelineLog_SharedWriterTerminalWithoutDSL(t *testing.T) {
cleanup := setupPipelineExecutorTestDB(t)
defer cleanup()
+ if err := dao.DB.Create(&entity.PipelineOperationLog{
+ ID: "run-1",
+ DocumentID: "doc-1",
+ TenantID: "tenant-1",
+ KbID: "kb-1",
+ OperationStatus: "5",
+ }).Error; err != nil {
+ t.Fatalf("seed pipeline log: %v", err)
+ }
docName := "terminal.pdf"
if err := RecordPipelineLog(t.Context(), dao.DB, PipelineLogInput{
@@ -387,6 +351,7 @@ func TestRecordPipelineLog_SharedWriterTerminalWithoutDSL(t *testing.T) {
Name: &docName,
Suffix: ".pdf",
},
+ PipelineLogID: "run-1",
}); err != nil {
t.Fatalf("RecordPipelineLog: %v", err)
}
@@ -403,99 +368,17 @@ func TestRecordPipelineLog_SharedWriterTerminalWithoutDSL(t *testing.T) {
}
}
-func TestRecordPipelineLog_BuiltinUsesParserIDFallback(t *testing.T) {
- cleanup := setupPipelineExecutorTestDB(t)
- defer cleanup()
-
- taskCtx := makeTaskCtx()
- taskCtx.Doc.ParserID = "general"
- taskCtx.Doc.Thumbnail = strPtr("thumb.png")
-
- var captured *entity.PipelineOperationLog
- svc := mustNewPipelineExecutor(t, taskCtx, "general", 0).
- WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- captured = log
- return nil
- })
- svc.recordPipelineLog(t.Context(), dao.DB, "doc-1", `{}`, "done")
-
- if captured == nil {
- t.Fatal("logCreateFunc was not called")
- }
- if captured.PipelineTitle == nil || *captured.PipelineTitle != "general" {
- t.Fatalf("PipelineTitle = %v, want \"general\"", captured.PipelineTitle)
- }
- if captured.Avatar == nil || *captured.Avatar != "thumb.png" {
- t.Fatalf("Avatar = %v, want \"thumb.png\"", captured.Avatar)
- }
- if captured.PipelineID != nil {
- t.Fatalf("PipelineID = %q, want nil for builtin pipeline", *captured.PipelineID)
- }
-}
-
-func TestRecordPipelineLog_CustomCanvasTitle(t *testing.T) {
- cleanup := setupPipelineExecutorTestDB(t)
- defer cleanup()
-
- if err := dao.DB.Create(&entity.UserCanvas{
- ID: "canvas-1",
- UserID: "tenant-1",
- Title: strPtr("My Pipeline"),
- Avatar: strPtr("a.png"),
- }).Error; err != nil {
- t.Fatalf("seed canvas: %v", err)
- }
-
- taskCtx := makeTaskCtx()
- taskCtx.Doc.ParserID = "general"
- taskCtx.PipelineID = "canvas-1"
-
- var captured *entity.PipelineOperationLog
- svc := mustNewPipelineExecutor(t, taskCtx, "canvas-1", 0).
- WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- captured = log
- return nil
- })
- svc.recordPipelineLog(t.Context(), dao.DB, "doc-1", `{}`, "done")
-
- if captured == nil {
- t.Fatal("logCreateFunc was not called")
- }
- if captured.PipelineTitle == nil || *captured.PipelineTitle != "My Pipeline" {
- t.Fatalf("PipelineTitle = %v, want \"My Pipeline\"", captured.PipelineTitle)
- }
- if captured.Avatar == nil || *captured.Avatar != "a.png" {
- t.Fatalf("Avatar = %v, want \"a.png\"", captured.Avatar)
- }
- if captured.PipelineID == nil || *captured.PipelineID != "canvas-1" {
- t.Fatalf("PipelineID = %v, want \"canvas-1\"", captured.PipelineID)
+func TestRecordPipelineLogRejectsMissingRunIdentity(t *testing.T) {
+ err := RecordPipelineLog(t.Context(), nil, PipelineLogInput{DocumentID: "doc-1", Status: "3"})
+ if !errors.Is(err, ErrMissingRunIdentity) {
+ t.Fatalf("RecordPipelineLog error = %v, want ErrMissingRunIdentity", err)
}
}
-func TestRecordPipelineLog_CustomCanvasMissingFallsBackToParserID(t *testing.T) {
- cleanup := setupPipelineExecutorTestDB(t)
- defer cleanup()
-
- taskCtx := makeTaskCtx()
- taskCtx.Doc.ParserID = "general"
- taskCtx.PipelineID = "canvas-gone"
-
- var captured *entity.PipelineOperationLog
- svc := mustNewPipelineExecutor(t, taskCtx, "canvas-gone", 0).
- WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- captured = log
- return nil
- })
- svc.recordPipelineLog(t.Context(), dao.DB, "doc-1", `{}`, "done")
-
- if captured == nil {
- t.Fatal("logCreateFunc was not called")
- }
- if captured.PipelineTitle == nil || *captured.PipelineTitle != "general" {
- t.Fatalf("PipelineTitle = %v, want \"general\" fallback", captured.PipelineTitle)
- }
- if captured.PipelineID == nil || *captured.PipelineID != "canvas-gone" {
- t.Fatalf("PipelineID = %v, want \"canvas-gone\"", captured.PipelineID)
+func TestRecordPipelineLogInternalWriterRejectsMissingRunIdentity(t *testing.T) {
+ err := recordPipelineLog(t.Context(), nil, PipelineLogInput{DocumentID: "doc-1", Status: "3"})
+ if !errors.Is(err, ErrMissingRunIdentity) {
+ t.Fatalf("recordPipelineLog error = %v, want ErrMissingRunIdentity", err)
}
}
@@ -520,6 +403,15 @@ func TestRecordPipelineLog_TerminalWithoutDSLResolvesCanvasTitle(t *testing.T) {
}).Error; err != nil {
t.Fatalf("seed knowledgebase: %v", err)
}
+ if err := dao.DB.Create(&entity.PipelineOperationLog{
+ ID: "run-1",
+ DocumentID: "doc-1",
+ TenantID: "tenant-1",
+ KbID: "kb-1",
+ OperationStatus: "5",
+ }).Error; err != nil {
+ t.Fatalf("seed pipeline log: %v", err)
+ }
docName := "sample.avi"
if err := dao.DB.Create(&entity.Document{
ID: "doc-1",
@@ -535,9 +427,10 @@ func TestRecordPipelineLog_TerminalWithoutDSLResolvesCanvasTitle(t *testing.T) {
// Mirrors Ingestor.recordTerminalPipelineLog: only the terminal status is
// known; pipeline_id and DSL are absent and must come from the document.
if err := RecordPipelineLog(t.Context(), dao.DB, PipelineLogInput{
- KbID: "kb-1",
- DocumentID: "doc-1",
- Status: "3",
+ KbID: "kb-1",
+ DocumentID: "doc-1",
+ Status: "3",
+ PipelineLogID: "run-1",
}); err != nil {
t.Fatalf("RecordPipelineLog: %v", err)
}
@@ -560,76 +453,6 @@ func TestRecordPipelineLog_TerminalWithoutDSLResolvesCanvasTitle(t *testing.T) {
}
}
-func TestRecordPipelineLog_SourceFrom(t *testing.T) {
- cases := []struct {
- name string
- sourceType string
- want string
- }{
- {name: "connector source strips connector id", sourceType: "rss/connector-811", want: "rss"},
- {name: "plain source unchanged", sourceType: "local", want: "local"},
- {name: "empty source unchanged", sourceType: "", want: ""},
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- taskCtx := makeTaskCtx()
- taskCtx.Doc.SourceType = tc.sourceType
- var captured *entity.PipelineOperationLog
- svc := mustNewPipelineExecutor(t, taskCtx, "flow-1", 0).WithLogCreateFunc(
- func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- captured = log
- return nil
- },
- )
- svc.recordPipelineLog(t.Context(), dao.DB, "doc-1", `{"components": {}}`, "done")
- if captured == nil {
- t.Fatal("logCreateFunc was not called")
- }
- if captured.SourceFrom != tc.want {
- t.Errorf("SourceFrom = %q, want %q", captured.SourceFrom, tc.want)
- }
- })
- }
-}
-
-// recordPipelineLog reloads the persisted document and derives source_from
-// from that row, so a stale task-context snapshot must not leak into the log.
-func TestRecordPipelineLog_SourceFromReloadedDoc(t *testing.T) {
- cleanup := setupPipelineExecutorTestDB(t)
- defer cleanup()
-
- persisted := &entity.Document{
- ID: "doc-1",
- KbID: "kb-1",
- ParserID: "naive",
- ParserConfig: entity.JSONMap{},
- SourceType: "rss/connector-811",
- Type: "pdf",
- CreatedBy: "tenant-1",
- Suffix: ".pdf",
- }
- if err := dao.NewDocumentDAO().Create(t.Context(), dao.DB, persisted); err != nil {
- t.Fatalf("seed document: %v", err)
- }
-
- taskCtx := makeTaskCtx()
- taskCtx.Doc.SourceType = "local"
- var captured *entity.PipelineOperationLog
- svc := mustNewPipelineExecutor(t, taskCtx, "flow-1", 0).WithLogCreateFunc(
- func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- captured = log
- return nil
- },
- )
- svc.recordPipelineLog(t.Context(), dao.DB, "doc-1", `{"components": {}}`, "done")
- if captured == nil {
- t.Fatal("logCreateFunc was not called")
- }
- if captured.SourceFrom != "rss" {
- t.Errorf("SourceFrom = %q, want %q", captured.SourceFrom, "rss")
- }
-}
-
func TestRecordPipelineLog_ReusesOpenPreTerminalRow(t *testing.T) {
cleanup := setupPipelineExecutorTestDB(t)
defer cleanup()
@@ -698,8 +521,8 @@ func TestRecordPipelineLog_ReusesOpenPreTerminalRow(t *testing.T) {
if log.Progress != progress {
t.Fatalf("Progress = %v, want %v", log.Progress, progress)
}
- if log.ProgressMsg == nil || *log.ProgressMsg != finalMsg {
- t.Fatalf("ProgressMsg = %v, want final message", log.ProgressMsg)
+ if log.ProgressMsg == nil || *log.ProgressMsg != queuedMsg {
+ t.Fatalf("ProgressMsg = %v, want unchanged queued message", log.ProgressMsg)
}
if len(log.DSL) != 0 {
t.Fatalf("DSL = %v, want empty object for terminal writer without DSL", log.DSL)
@@ -837,179 +660,6 @@ func TestRecordPipelineLog_DoesNotAdoptAnotherRunsRow(t *testing.T) {
}
}
-// TestRecordPipelineLog_UnboundAdoptsOpenRow keeps the legacy contract for
-// callers that carry no bound row (debug-adjacent and non-ingestion paths): the
-// document's newest open row is adopted so a stray queued entry is closed
-// rather than left open.
-func TestRecordPipelineLog_UnboundAdoptsOpenRow(t *testing.T) {
- cleanup := setupPipelineExecutorTestDB(t)
- defer cleanup()
-
- openMsg := "Task is queued..."
- open := &entity.PipelineOperationLog{
- ID: "open-log",
- DocumentID: "doc-1",
- TenantID: "tenant-1",
- KbID: "kb-1",
- ParserID: "naive",
- TaskType: "Parse",
- OperationStatus: "5",
- ProgressMsg: &openMsg,
- }
- if err := dao.DB.Create(open).Error; err != nil {
- t.Fatalf("seed open log: %v", err)
- }
-
- finalMsg := "Parser Done"
- if err := dao.DB.Create(&entity.Document{
- ID: "doc-1",
- KbID: "kb-1",
- ParserID: "naive",
- ParserConfig: entity.JSONMap{},
- SourceType: "local",
- Type: "pdf",
- CreatedBy: "tenant-1",
- Suffix: ".pdf",
- ProgressMsg: &finalMsg,
- }).Error; err != nil {
- t.Fatalf("seed document: %v", err)
- }
-
- if err := RecordPipelineLog(t.Context(), dao.DB, PipelineLogInput{
- TenantID: "tenant-1",
- KbID: "kb-1",
- DocumentID: "doc-1",
- Status: "3",
- }); err != nil {
- t.Fatalf("RecordPipelineLog: %v", err)
- }
-
- var reloaded entity.PipelineOperationLog
- if err := dao.DB.First(&reloaded, "id = ?", "open-log").Error; err != nil {
- t.Fatalf("reload open log: %v", err)
- }
- if reloaded.OperationStatus != "3" {
- t.Fatalf("open row OperationStatus = %q, want %q (adopted by the unbound writer)", reloaded.OperationStatus, "3")
- }
- var count int64
- if err := dao.DB.Model(&entity.PipelineOperationLog{}).Where("document_id = ?", "doc-1").Count(&count).Error; err != nil {
- t.Fatalf("count pipeline logs: %v", err)
- }
- if count != 1 {
- t.Fatalf("pipeline log rows = %d, want 1 (adopted, not duplicated)", count)
- }
-}
-
-// TestRecordPipelineLog_DoesNotAdoptLiveRunsOpenRow locks the ownership rule on
-// the unbound fallback: the document's newest open row belongs to a live run, so
-// another run's terminal write must not finalize it. Adopting it would stamp the
-// live run's entry with a foreign status and swallow that run's own terminal
-// write (its CAS would find the row already closed).
-func TestRecordPipelineLog_DoesNotAdoptLiveRunsOpenRow(t *testing.T) {
- cleanup := setupPipelineExecutorTestDB(t)
- defer cleanup()
-
- liveMsg := "Task is queued..."
- if err := dao.DB.Create(&entity.PipelineOperationLog{
- ID: "live-run-log",
- DocumentID: "doc-1",
- TenantID: "tenant-1",
- KbID: "kb-1",
- ParserID: "naive",
- TaskType: "Parse",
- OperationStatus: "5",
- ProgressMsg: &liveMsg,
- }).Error; err != nil {
- t.Fatalf("seed live run's open log: %v", err)
- }
- if err := dao.DB.Create(&entity.IngestionTask{
- ID: "live-task",
- UserID: "user-1",
- DocumentID: "doc-1",
- DatasetID: "kb-1",
- Status: "RUNNING",
- }).Error; err != nil {
- t.Fatalf("seed live task: %v", err)
- }
- if err := dao.DB.Model(&entity.IngestionTask{}).Where("id = ?", "live-task").
- Update("pipeline_log_id", "live-run-log").Error; err != nil {
- t.Fatalf("bind live task: %v", err)
- }
-
- finalMsg := "Parser Done"
- if err := dao.DB.Create(&entity.Document{
- ID: "doc-1",
- KbID: "kb-1",
- ParserID: "naive",
- ParserConfig: entity.JSONMap{},
- SourceType: "local",
- Type: "pdf",
- CreatedBy: "tenant-1",
- Suffix: ".pdf",
- ProgressMsg: &finalMsg,
- }).Error; err != nil {
- t.Fatalf("seed document: %v", err)
- }
-
- if err := RecordPipelineLog(t.Context(), dao.DB, PipelineLogInput{
- TenantID: "tenant-1",
- KbID: "kb-1",
- DocumentID: "doc-1",
- Status: "4",
- }); err != nil {
- t.Fatalf("RecordPipelineLog: %v", err)
- }
-
- var live entity.PipelineOperationLog
- if err := dao.DB.First(&live, "id = ?", "live-run-log").Error; err != nil {
- t.Fatalf("load live run's log: %v", err)
- }
- if live.OperationStatus != "5" {
- t.Fatalf("live run's row was adopted: OperationStatus = %q, want it untouched at %q", live.OperationStatus, "5")
- }
- var count int64
- if err := dao.DB.Model(&entity.PipelineOperationLog{}).Where("document_id = ?", "doc-1").Count(&count).Error; err != nil {
- t.Fatalf("count pipeline logs: %v", err)
- }
- if count != 2 {
- t.Fatalf("pipeline log rows = %d, want 2 (the unbound run records its own entry)", count)
- }
-}
-
-func TestRecordPipelineLog_CreatesRowWithoutOpenPreTerminalRow(t *testing.T) {
- cleanup := setupPipelineExecutorTestDB(t)
- defer cleanup()
-
- docName := "legacy.pdf"
- if err := dao.DB.Create(&entity.Document{
- ID: "doc-1",
- KbID: "kb-1",
- ParserID: "naive",
- ParserConfig: entity.JSONMap{},
- SourceType: "local",
- Type: "pdf",
- CreatedBy: "tenant-1",
- Name: &docName,
- Suffix: ".pdf",
- }).Error; err != nil {
- t.Fatalf("seed document: %v", err)
- }
-
- taskCtx := makeTaskCtx()
- taskCtx.Doc.ParserID = "naive"
- var captured *entity.PipelineOperationLog
- svc := mustNewPipelineExecutor(t, taskCtx, "flow-1", 0).WithLogCreateFunc(
- func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- captured = log
- return nil
- },
- )
- svc.recordPipelineLog(t.Context(), dao.DB, "doc-1", `{"components": {}}`, "done")
- if captured == nil {
- t.Fatal("logCreateFunc was not called: expected Create fallback without an open row")
- }
-}
-
// =============================================================================
// updateDocumentMetadata
// =============================================================================
@@ -1024,9 +674,7 @@ func TestRunPipeline_NilOutput(t *testing.T) {
}
func TestRunPipeline_EmptyOutput(t *testing.T) {
- svc := mustNewPipelineExecutor(t, makeTaskCtx(), "flow-1", 0).WithLogCreateFunc(
- func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error { return nil },
- )
+ svc := mustNewPipelineExecutor(t, makeTaskCtx(), "flow-1", 0)
ctx := t.Context()
_, err := svc.processOutput(ctx, map[string]any{}, time.Now())
if err != nil {
@@ -1035,9 +683,7 @@ func TestRunPipeline_EmptyOutput(t *testing.T) {
}
func TestRunPipeline_NormalizedEmpty(t *testing.T) {
- svc := mustNewPipelineExecutor(t, makeTaskCtx(), "flow-1", 0).WithLogCreateFunc(
- func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error { return nil },
- )
+ svc := mustNewPipelineExecutor(t, makeTaskCtx(), "flow-1", 0)
ctx := t.Context()
_, err := svc.processOutput(ctx, map[string]any{"markdown": ""}, time.Now())
if err != nil {
@@ -1049,8 +695,7 @@ func TestRunPipeline_FullFlow(t *testing.T) {
svc := mustNewPipelineExecutor(t, makeTaskCtx(), "flow-1", 0).
WithInsertFunc(func(ctx context.Context, chunks []map[string]any, baseName, datasetID string) ([]string, error) {
return nil, nil
- }).
- WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error { return nil })
+ })
output := map[string]any{
"chunks": []map[string]any{
{"text": "hello"},
@@ -1068,8 +713,7 @@ func TestRunPipeline_AlreadyHasVectors(t *testing.T) {
svc := mustNewPipelineExecutor(t, makeTaskCtx(), "flow-1", 0).
WithInsertFunc(func(ctx context.Context, chunks []map[string]any, baseName, datasetID string) ([]string, error) {
return nil, nil
- }).
- WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error { return nil })
+ })
output := map[string]any{
"chunks": []map[string]any{
@@ -1097,7 +741,6 @@ func TestRunPipeline_ContextCanceled(t *testing.T) {
}
func TestPipelineExecutor_Run_MainFlowWithStubs(t *testing.T) {
- logged := false
inserted := false
taskCtx := makeTaskCtx()
@@ -1117,13 +760,6 @@ func TestPipelineExecutor_Run_MainFlowWithStubs(t *testing.T) {
WithInsertFunc(func(ctx context.Context, chunks []map[string]any, baseName, datasetID string) ([]string, error) {
inserted = true
return nil, nil
- }).
- WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- logged = true
- if log.PipelineID == nil || *log.PipelineID != "flow-corrected" {
- t.Fatalf("PipelineID = %v, want flow-corrected", log.PipelineID)
- }
- return nil
})
_, err := svc.Execute(context.Background())
@@ -1133,53 +769,34 @@ func TestPipelineExecutor_Run_MainFlowWithStubs(t *testing.T) {
if !inserted {
t.Fatal("expected insertChunks to be called")
}
- if !logged {
- t.Fatal("expected pipeline log to be created")
- }
}
func TestPipelineExecutor_Execute_DoesNotLogFailedRun(t *testing.T) {
- logged := false
svc := mustNewPipelineExecutor(t, makeTaskCtx(), "flow-1", 0).
WithLoadDSLFunc(func(ctx context.Context, canvasID string) (string, string, error) {
return `{"nodes":[{"id":"n1"}],"edges":[]}`, canvasID, nil
}).
WithRunPipelineFunc(func(ctx context.Context, dsl string) (map[string]any, string, error) {
return nil, dsl, errors.New("pipeline failed")
- }).
- WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- logged = true
- return nil
})
if _, err := svc.Execute(context.Background()); err == nil {
t.Fatal("Execute error = nil, want failure")
}
- if logged {
- t.Fatal("executor must not log failed runs before ingestor writes final document status")
- }
}
func TestPipelineExecutor_Execute_DoesNotLogCanceledRun(t *testing.T) {
- logged := false
svc := mustNewPipelineExecutor(t, makeTaskCtx(), "flow-1", 0).
WithLoadDSLFunc(func(ctx context.Context, canvasID string) (string, string, error) {
return `{"nodes":[{"id":"n1"}],"edges":[]}`, canvasID, nil
}).
WithRunPipelineFunc(func(ctx context.Context, dsl string) (map[string]any, string, error) {
return nil, dsl, context.Canceled
- }).
- WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- logged = true
- return nil
})
if _, err := svc.Execute(context.Background()); !errors.Is(err, context.Canceled) {
t.Fatalf("Execute error = %v, want context.Canceled", err)
}
- if logged {
- t.Fatal("executor must not log canceled runs before ingestor writes final document status")
- }
}
// TestPipelineExecutor_Execute_PropagatesContext verifies the ctx passed to
@@ -1204,8 +821,7 @@ func TestPipelineExecutor_Execute_PropagatesContext(t *testing.T) {
}).
WithInsertFunc(func(ctx context.Context, chunks []map[string]any, baseName, datasetID string) ([]string, error) {
return nil, nil
- }).
- WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error { return nil })
+ })
if _, err := svc.Execute(taskCtx.Ctx); err != nil {
t.Fatalf("unexpected error: %v", err)
@@ -1215,7 +831,6 @@ func TestPipelineExecutor_Execute_PropagatesContext(t *testing.T) {
func TestPipelineExecutor_Execute_RecordsDoneOperationStatus(t *testing.T) {
taskCtx := makeTaskCtx()
taskCtx.Ctx = t.Context()
- var capturedLog *entity.PipelineOperationLog
svc := mustNewPipelineExecutor(t, taskCtx, "flow-1", 0).
WithLoadDSLFunc(func(ctx context.Context, canvasID string) (string, string, error) {
@@ -1226,24 +841,11 @@ func TestPipelineExecutor_Execute_RecordsDoneOperationStatus(t *testing.T) {
}).
WithInsertFunc(func(ctx context.Context, chunks []map[string]any, baseName, datasetID string) ([]string, error) {
return nil, nil
- }).
- WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error {
- capturedLog = log
- return nil
})
if _, err := svc.Execute(taskCtx.Ctx); err != nil {
t.Fatalf("unexpected error: %v", err)
}
- if capturedLog == nil {
- t.Fatalf("expected pipeline operation log to be recorded")
- }
- if capturedLog.OperationStatus == "" {
- t.Fatalf("expected OperationStatus to be non-empty")
- }
- if capturedLog.OperationStatus != string(entity.TaskStatusDone) {
- t.Fatalf("expected OperationStatus = %q, got %q", entity.TaskStatusDone, capturedLog.OperationStatus)
- }
}
// =============================================================================
diff --git a/internal/ingestion/task/pipeline_real_integration_test.go b/internal/ingestion/task/pipeline_real_integration_test.go
index 145eece9d1..e82ba34ed1 100644
--- a/internal/ingestion/task/pipeline_real_integration_test.go
+++ b/internal/ingestion/task/pipeline_real_integration_test.go
@@ -242,8 +242,7 @@ func TestPipelineExecutor_Run_RealPDF_ProducesIndexedChunks(t *testing.T) {
WithInsertFunc(func(ctx context.Context, chunks []map[string]any, baseName, datasetID string) ([]string, error) {
inserted = append(inserted, deepCopyTaskChunks(chunks))
return nil, nil
- }).
- WithLogCreateFunc(func(ctx context.Context, db *gorm.DB, log *entity.PipelineOperationLog) error { return nil })
+ })
if _, err = svc.Execute(ctx); err != nil {
t.Fatalf("Run: %v", err)
diff --git a/internal/ingestion/testutil/helpers.go b/internal/ingestion/testutil/helpers.go
index 7f548ae371..78c4a295a9 100644
--- a/internal/ingestion/testutil/helpers.go
+++ b/internal/ingestion/testutil/helpers.go
@@ -64,6 +64,7 @@ func SetupTestDB(t *testing.T, tables ...any) *gorm.DB {
tables = []any{
&entity.IngestionTask{},
&entity.IngestionTaskLog{},
+ &entity.PipelineOperationLog{},
&entity.Task{},
&entity.Document{},
&entity.Knowledgebase{},
@@ -208,12 +209,27 @@ func SeedTestData(t *testing.T, db *gorm.DB, opts ...TestDataOption) (string, st
}
// Create IngestionTask
+ runCount := 1
+ runID := "run-" + cfg.taskID
+ if err := db.Create(&entity.PipelineOperationLog{
+ ID: runID,
+ DocumentID: cfg.docID,
+ RunCount: &runCount,
+ TenantID: cfg.tenantID,
+ KbID: cfg.kbID,
+ ParserID: doc.ParserID,
+ TaskType: string(entity.PipelineTaskTypeParse),
+ OperationStatus: string(entity.TaskStatusRunning),
+ }).Error; err != nil {
+ t.Fatalf("create pipeline operation log: %v", err)
+ }
if err := db.Create(&entity.IngestionTask{
- ID: cfg.taskID,
- UserID: "u1",
- DocumentID: cfg.docID,
- DatasetID: cfg.kbID,
- Status: common.RUNNING,
+ ID: cfg.taskID,
+ UserID: "u1",
+ DocumentID: cfg.docID,
+ DatasetID: cfg.kbID,
+ Status: common.RUNNING,
+ PipelineLogID: &runID,
}).Error; err != nil {
t.Fatalf("create ingestion task: %v", err)
}
diff --git a/internal/router/router.go b/internal/router/router.go
index 51c647d4cc..00a0fbab83 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -388,6 +388,7 @@ func (r *Router) Setup(engine *gin.Engine) {
// Dataset ingestion logs
datasets.GET("/:dataset_id/ingestions/summary", r.datasetsHandler.GetIngestionSummary)
datasets.GET("/:dataset_id/ingestions", r.datasetsHandler.ListIngestionLogs)
+ datasets.GET("/:dataset_id/ingestions/:log_id/messages", r.datasetsHandler.ListIngestionMessages)
datasets.GET("/:dataset_id/ingestions/:log_id", r.datasetsHandler.GetIngestionLog)
// Metadata Config
diff --git a/internal/service/dataset/ingestion.go b/internal/service/dataset/ingestion.go
index d64e170686..20b65431ca 100644
--- a/internal/service/dataset/ingestion.go
+++ b/internal/service/dataset/ingestion.go
@@ -8,8 +8,27 @@ import (
"ragflow/internal/common"
"ragflow/internal/dao"
"ragflow/internal/entity"
+ "ragflow/internal/service"
)
+const (
+ defaultIngestionMessagesLimit = 200
+ maxIngestionMessagesLimit = 500
+)
+
+// IngestionMessagesResponse is one immutable run's keyset-paginated event
+// stream. IDs are database event IDs and must be returned unchanged by clients
+// when requesting adjacent pages.
+type IngestionMessagesResponse struct {
+ RunCount int `json:"run_count"`
+ Items []service.IngestionEventItem `json:"items"`
+ OldestID int `json:"oldest_id"`
+ NewestID int `json:"newest_id"`
+ HasMoreBefore bool `json:"has_more_before"`
+ HasMoreAfter bool `json:"has_more_after"`
+ Terminal bool `json:"terminal"`
+}
+
func (d *DatasetService) GetIngestionSummary(ctx context.Context, datasetID, userID string) (map[string]interface{}, common.ErrorCode, error) {
if datasetID == "" {
return nil, common.CodeDataError, errors.New(`lack of "Dataset ID"`)
@@ -39,6 +58,74 @@ func (d *DatasetService) GetIngestionSummary(ctx context.Context, datasetID, use
}, common.CodeSuccess, nil
}
+// ListIngestionMessages returns events owned by the requested immutable
+// pipeline log.
+func (d *DatasetService) ListIngestionMessages(ctx context.Context, datasetID, userID, logID string, limit int, afterID, beforeID *int) (*IngestionMessagesResponse, common.ErrorCode, error) {
+ if datasetID == "" {
+ return nil, common.CodeArgumentError, errors.New(`lack of "Dataset ID"`)
+ }
+ if logID == "" {
+ return nil, common.CodeArgumentError, errors.New(`lack of "Log ID"`)
+ }
+ if afterID != nil && beforeID != nil {
+ return nil, common.CodeArgumentError, errors.New("after_id and before_id are mutually exclusive")
+ }
+ if afterID != nil && *afterID <= 0 {
+ return nil, common.CodeArgumentError, errors.New("after_id must be a positive integer")
+ }
+ if beforeID != nil && *beforeID <= 0 {
+ return nil, common.CodeArgumentError, errors.New("before_id must be a positive integer")
+ }
+ if limit == 0 {
+ limit = defaultIngestionMessagesLimit
+ }
+ if limit < 0 || limit > maxIngestionMessagesLimit {
+ return nil, common.CodeArgumentError, fmt.Errorf("limit must be between 1 and %d", maxIngestionMessagesLimit)
+ }
+ if !d.kbDAO.Accessible(ctx, dao.DB, datasetID, userID) {
+ return nil, common.CodeDataError, errors.New("no authorization")
+ }
+
+ run, err := d.pipelineLogDAO.GetByIDAndKBID(ctx, dao.DB, logID, datasetID)
+ if err != nil {
+ if dao.IsNotFoundErr(err) {
+ return nil, common.CodeDataError, errors.New("log not found")
+ }
+ return nil, common.CodeServerError, fmt.Errorf("get ingestion log: %w", err)
+ }
+ if run.RunCount == nil || *run.RunCount <= 0 {
+ return nil, common.CodeDataError, errors.New("log not found")
+ }
+ response := &IngestionMessagesResponse{}
+ response.RunCount = *run.RunCount
+ response.Terminal = isTerminalIngestionLogStatus(run.OperationStatus)
+
+ page, err := dao.NewIngestionTaskLogDAO().ListEventsPageByPipelineLogID(ctx, dao.DB, logID, limit, afterID, beforeID)
+ if err != nil {
+ return nil, common.CodeServerError, fmt.Errorf("list ingestion messages: %w", err)
+ }
+ response.Items = make([]service.IngestionEventItem, 0, len(page.Events))
+ for _, event := range page.Events {
+ response.Items = append(response.Items, service.IngestionEventItemFromLog(event))
+ }
+ if len(response.Items) > 0 {
+ response.OldestID = response.Items[0].ID
+ response.NewestID = response.Items[len(response.Items)-1].ID
+ }
+ response.HasMoreBefore = page.HasMoreBefore
+ response.HasMoreAfter = page.HasMoreAfter
+ return response, common.CodeSuccess, nil
+}
+
+func isTerminalIngestionLogStatus(status string) bool {
+ switch status {
+ case string(entity.TaskStatusDone), string(entity.TaskStatusFail), string(entity.TaskStatusCancel), "DONE", "FAIL", "CANCEL":
+ return true
+ default:
+ return false
+ }
+}
+
func (d *DatasetService) ListIngestionLogs(ctx context.Context, datasetID, userID string, page, pageSize int, terms []dao.OrderTerm, operationStatus []string, createDateFrom, createDateTo, logType, keywords, documentID string) (map[string]interface{}, common.ErrorCode, error) {
if datasetID == "" {
return nil, common.CodeDataError, errors.New(`lack of "Dataset ID"`)
@@ -67,16 +154,27 @@ func (d *DatasetService) ListIngestionLogs(ctx context.Context, datasetID, userI
if err != nil {
return nil, common.CodeServerError, fmt.Errorf("list ingestion logs: %w", err)
}
+ logIDs := make([]string, 0, len(logs))
+ for _, log := range logs {
+ if log != nil && log.ID != "" {
+ logIDs = append(logIDs, log.ID)
+ }
+ }
+ latestEvents, err := dao.NewIngestionTaskLogDAO().LatestEventsByPipelineLogIDs(ctx, dao.DB, logIDs)
+ if err != nil {
+ return nil, common.CodeServerError, fmt.Errorf("list latest ingestion events: %w", err)
+ }
items := make([]map[string]interface{}, 0, len(logs))
for _, log := range logs {
if log == nil {
continue
}
+ latestEvent := ingestionEventItem(latestEvents[log.ID])
if logType == "file" {
- items = append(items, fileIngestionLogToMap(log))
+ items = append(items, fileIngestionLogToMap(log, latestEvent))
} else {
- items = append(items, datasetIngestionLogToMap(log))
+ items = append(items, datasetIngestionLogToMap(log, latestEvent))
}
}
@@ -104,69 +202,81 @@ func (d *DatasetService) GetIngestionLog(ctx context.Context, datasetID, userID,
}
return nil, common.CodeServerError, fmt.Errorf("get ingestion log: %w", err)
}
+ if log.RunCount == nil || *log.RunCount <= 0 {
+ return nil, common.CodeDataError, errors.New("log not found")
+ }
- return datasetIngestionLogToMap(log), common.CodeSuccess, nil
+ latestEvents, err := dao.NewIngestionTaskLogDAO().LatestEventsByPipelineLogIDs(ctx, dao.DB, []string{log.ID})
+ if err != nil {
+ return nil, common.CodeServerError, fmt.Errorf("get latest ingestion event: %w", err)
+ }
+ return datasetIngestionLogToMap(log, ingestionEventItem(latestEvents[log.ID])), common.CodeSuccess, nil
}
-func datasetIngestionLogToMap(log *entity.PipelineOperationLog) map[string]interface{} {
+func datasetIngestionLogToMap(log *entity.PipelineOperationLog, latestEvent *service.IngestionEventItem) map[string]interface{} {
m := map[string]interface{}{
- "id": log.ID,
- "dataset_id": log.KbID,
- "tenant_id": log.TenantID,
- "document_id": log.DocumentID,
- "document_name": log.DocumentName,
- "document_suffix": log.DocumentSuffix,
- "source_from": log.SourceFrom,
- "task_type": log.TaskType,
- "operation_status": log.OperationStatus,
- "progress": log.Progress,
- "process_begin_at": log.ProcessBeginAt,
- "process_duration": log.ProcessDuration,
- "progress_msg": log.ProgressMsg,
- "dsl": log.DSL,
- "avatar": log.Avatar,
- "create_time": log.CreateTime,
- "create_date": log.CreateDate,
- "update_time": log.UpdateTime,
- "update_date": log.UpdateDate,
+ "id": log.ID,
+ "dataset_id": log.KbID,
+ "tenant_id": log.TenantID,
+ "document_id": log.DocumentID,
+ "document_name": log.DocumentName,
+ "document_suffix": log.DocumentSuffix,
+ "source_from": log.SourceFrom,
+ "task_type": log.TaskType,
+ "operation_status": log.OperationStatus,
+ "progress": log.Progress,
+ "process_begin_at": log.ProcessBeginAt,
+ "process_duration": log.ProcessDuration,
+ "dsl": log.DSL,
+ "avatar": log.Avatar,
+ "create_time": log.CreateTime,
+ "create_date": log.CreateDate,
+ "update_time": log.UpdateTime,
+ "update_date": log.UpdateDate,
+ "latest_ingestion_event": latestEvent,
}
if log.PipelineID != nil {
m["pipeline_id"] = *log.PipelineID
}
- if log.ProgressMsg != nil {
- m["progress_msg"] = *log.ProgressMsg
- }
if log.Status != nil {
m["status"] = *log.Status
}
return m
}
-func fileIngestionLogToMap(log *entity.PipelineOperationLog) map[string]interface{} {
+func fileIngestionLogToMap(log *entity.PipelineOperationLog, latestEvent *service.IngestionEventItem) map[string]interface{} {
return map[string]interface{}{
- "id": log.ID,
- "document_id": log.DocumentID,
- "tenant_id": log.TenantID,
- "kb_id": log.KbID,
- "pipeline_id": stringPointerValue(log.PipelineID),
- "pipeline_title": stringPointerValue(log.PipelineTitle),
- "parser_id": log.ParserID,
- "document_name": log.DocumentName,
- "document_suffix": log.DocumentSuffix,
- "document_type": log.DocumentType,
- "source_from": log.SourceFrom,
- "progress": log.Progress,
- "progress_msg": stringPointerValue(log.ProgressMsg),
- "process_begin_at": timePointerValue(log.ProcessBeginAt),
- "process_duration": log.ProcessDuration,
- "dsl": jsonMapValue(log.DSL),
- "task_type": log.TaskType,
- "operation_status": log.OperationStatus,
- "avatar": stringPointerValue(log.Avatar),
- "status": stringPointerValue(log.Status),
- "create_time": int64PointerValue(log.CreateTime),
- "create_date": timePointerValue(log.CreateDate),
- "update_time": int64PointerValue(log.UpdateTime),
- "update_date": timePointerValue(log.UpdateDate),
+ "id": log.ID,
+ "document_id": log.DocumentID,
+ "tenant_id": log.TenantID,
+ "kb_id": log.KbID,
+ "pipeline_id": stringPointerValue(log.PipelineID),
+ "pipeline_title": stringPointerValue(log.PipelineTitle),
+ "parser_id": log.ParserID,
+ "document_name": log.DocumentName,
+ "document_suffix": log.DocumentSuffix,
+ "document_type": log.DocumentType,
+ "source_from": log.SourceFrom,
+ "progress": log.Progress,
+ "process_begin_at": timePointerValue(log.ProcessBeginAt),
+ "process_duration": log.ProcessDuration,
+ "dsl": jsonMapValue(log.DSL),
+ "task_type": log.TaskType,
+ "operation_status": log.OperationStatus,
+ "avatar": stringPointerValue(log.Avatar),
+ "status": stringPointerValue(log.Status),
+ "create_time": int64PointerValue(log.CreateTime),
+ "create_date": timePointerValue(log.CreateDate),
+ "update_time": int64PointerValue(log.UpdateTime),
+ "update_date": timePointerValue(log.UpdateDate),
+ "latest_ingestion_event": latestEvent,
+ }
+}
+
+func ingestionEventItem(event *entity.IngestionTaskLog) *service.IngestionEventItem {
+ if event == nil {
+ return nil
}
+ item := service.IngestionEventItemFromLog(event)
+ return &item
}
diff --git a/internal/service/dataset/ingestion_messages_test.go b/internal/service/dataset/ingestion_messages_test.go
new file mode 100644
index 0000000000..1472665fd2
--- /dev/null
+++ b/internal/service/dataset/ingestion_messages_test.go
@@ -0,0 +1,233 @@
+package dataset
+
+import (
+ "testing"
+
+ "ragflow/internal/common"
+ "ragflow/internal/dao"
+ "ragflow/internal/entity"
+ "ragflow/internal/service"
+
+ "gorm.io/gorm"
+)
+
+func TestListIngestionMessagesUsesRunScopedKeysets(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ if err := db.AutoMigrate(&entity.PipelineOperationLog{}); err != nil {
+ t.Fatalf("migrate pipeline operation log: %v", err)
+ }
+ insertCompilationOwnerKB(t, "kb-1", "user-1")
+ insertMessageRun(t, "run-1", "kb-1", "doc-1", entity.TaskStatusRunning, 2)
+ for i := 0; i < 5; i++ {
+ insertMessageEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, "detail")
+ }
+
+ response, code, err := NewDatasetService().ListIngestionMessages(t.Context(), "kb-1", "user-1", "run-1", 2, nil, nil)
+ if err != nil || code != common.CodeSuccess {
+ t.Fatalf("ListIngestionMessages = (%+v, %v, %v), want success", response, code, err)
+ }
+ if response.RunCount != 2 || response.OldestID != 4 || response.NewestID != 5 || !response.HasMoreBefore || response.HasMoreAfter || response.Terminal {
+ t.Fatalf("first response = %+v, want newest page for active run", response)
+ }
+ assertMessageEventIDs(t, response.Items, 4, 5)
+
+ afterID := 2
+ response, code, err = NewDatasetService().ListIngestionMessages(t.Context(), "kb-1", "user-1", "run-1", 2, &afterID, nil)
+ if err != nil || code != common.CodeSuccess {
+ t.Fatalf("after page = (%+v, %v, %v), want success", response, code, err)
+ }
+ assertMessageEventIDs(t, response.Items, 3, 4)
+ if !response.HasMoreBefore || !response.HasMoreAfter {
+ t.Fatalf("after response flags = before:%t after:%t, want true/true", response.HasMoreBefore, response.HasMoreAfter)
+ }
+
+ beforeID := 4
+ if _, code, err := NewDatasetService().ListIngestionMessages(t.Context(), "kb-1", "user-1", "run-1", 2, &afterID, &beforeID); err == nil || code != common.CodeArgumentError {
+ t.Fatalf("mutually exclusive cursors = code:%v err:%v, want argument error", code, err)
+ }
+}
+
+func TestListIngestionMessagesRejectsUnnumberedRun(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ if err := db.AutoMigrate(&entity.PipelineOperationLog{}); err != nil {
+ t.Fatalf("migrate pipeline operation log: %v", err)
+ }
+ insertCompilationOwnerKB(t, "kb-1", "user-1")
+ insertMessageRun(t, "legacy-run", "kb-1", "doc-1", entity.TaskStatusRunning, 0)
+ insertMessageEvent(t, db, "legacy-run", "task-1", dao.EventTypeMessage, "old detail")
+
+ response, code, err := NewDatasetService().ListIngestionMessages(t.Context(), "kb-1", "user-1", "legacy-run", 200, nil, nil)
+ if response != nil || err == nil || code != common.CodeDataError {
+ t.Fatalf("ListIngestionMessages = (%+v, %v, %v), want not-found data error", response, code, err)
+ }
+}
+
+func TestListIngestionLogsExcludesUnnumberedRuns(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ if err := db.AutoMigrate(&entity.PipelineOperationLog{}); err != nil {
+ t.Fatalf("migrate pipeline operation log: %v", err)
+ }
+ insertCompilationOwnerKB(t, "kb-1", "user-1")
+ insertMessageRun(t, "run-1", "kb-1", "doc-1", entity.TaskStatusDone, 1)
+ insertMessageRun(t, "old-run", "kb-1", "doc-2", entity.TaskStatusDone, 0)
+
+ result, code, err := NewDatasetService().ListIngestionLogs(t.Context(), "kb-1", "user-1", 1, 30, nil, nil, "", "", "file", "", "")
+ if err != nil || code != common.CodeSuccess {
+ t.Fatalf("ListIngestionLogs = (%+v, %v, %v), want success", result, code, err)
+ }
+ logs, ok := result["logs"].([]map[string]interface{})
+ if !ok || len(logs) != 1 || logs[0]["id"] != "run-1" {
+ t.Fatalf("logs = %#v, want only numbered run", result["logs"])
+ }
+}
+
+func TestListIngestionLogsEmbedsLatestRunEventWithoutProgressMessage(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ if err := db.AutoMigrate(&entity.PipelineOperationLog{}); err != nil {
+ t.Fatalf("migrate pipeline operation log: %v", err)
+ }
+ insertCompilationOwnerKB(t, "kb-1", "user-1")
+ insertMessageRun(t, "run-1", "kb-1", "doc-1", entity.TaskStatusDone, 1)
+ insertMessageRun(t, "run-2", "kb-1", "doc-2", entity.TaskStatusDone, 1)
+ if err := db.Model(&entity.PipelineOperationLog{}).Where("id IN ?", []string{"run-1", "run-2"}).Update("progress_msg", "obsolete snapshot").Error; err != nil {
+ t.Fatalf("seed obsolete progress message: %v", err)
+ }
+ insertMessageEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, "first")
+ insertMessageEvent(t, db, "run-1", "task-1", dao.EventTypeTerminal, "latest first")
+ insertMessageEvent(t, db, "run-2", "task-2", dao.EventTypeMessage, "latest second")
+
+ result, code, err := NewDatasetService().ListIngestionLogs(t.Context(), "kb-1", "user-1", 1, 30, nil, nil, "", "", "file", "", "")
+ if err != nil || code != common.CodeSuccess {
+ t.Fatalf("ListIngestionLogs = (%+v, %v, %v), want success", result, code, err)
+ }
+ logs, ok := result["logs"].([]map[string]interface{})
+ if !ok || len(logs) != 2 {
+ t.Fatalf("logs = %#v, want two file logs", result["logs"])
+ }
+ byID := make(map[string]map[string]interface{}, len(logs))
+ for _, log := range logs {
+ byID[log["id"].(string)] = log
+ }
+ assertLatestEventMap(t, byID["run-1"], 2, "latest first")
+ assertLatestEventMap(t, byID["run-2"], 3, "latest second")
+ for runID, log := range byID {
+ if _, ok := log["progress_msg"]; ok {
+ t.Fatalf("%s exposes obsolete progress_msg: %#v", runID, log["progress_msg"])
+ }
+ }
+}
+
+func TestGetIngestionLogEmbedsLatestRunEvent(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ if err := db.AutoMigrate(&entity.PipelineOperationLog{}); err != nil {
+ t.Fatalf("migrate pipeline operation log: %v", err)
+ }
+ insertCompilationOwnerKB(t, "kb-1", "user-1")
+ insertMessageRun(t, "run-1", "kb-1", "doc-1", entity.TaskStatusDone, 1)
+ insertMessageEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, "latest detail")
+
+ result, code, err := NewDatasetService().GetIngestionLog(t.Context(), "kb-1", "user-1", "run-1")
+ if err != nil || code != common.CodeSuccess {
+ t.Fatalf("GetIngestionLog = (%+v, %v, %v), want success", result, code, err)
+ }
+ assertLatestEventMap(t, result, 1, "latest detail")
+}
+
+func TestIngestionEventWriterFeedsRunScopedLogReaders(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ if err := db.AutoMigrate(&entity.PipelineOperationLog{}); err != nil {
+ t.Fatalf("migrate pipeline operation log: %v", err)
+ }
+ insertCompilationOwnerKB(t, "kb-1", "user-1")
+ insertMessageRun(t, "run-1", "kb-1", "doc-1", entity.TaskStatusRunning, 1)
+
+ writer := service.NewIngestionTaskService()
+ if err := writer.RecordLifecycle(t.Context(), "run-1", "task-1", "Parser", 0, "Parser started"); err != nil {
+ t.Fatalf("record lifecycle event: %v", err)
+ }
+ if err := writer.RecordMessage(t.Context(), "run-1", "task-1", "Parsing pages 2/8"); err != nil {
+ t.Fatalf("record progress event: %v", err)
+ }
+
+ messages, code, err := NewDatasetService().ListIngestionMessages(t.Context(), "kb-1", "user-1", "run-1", 200, nil, nil)
+ if err != nil || code != common.CodeSuccess {
+ t.Fatalf("ListIngestionMessages = (%+v, %v, %v), want success", messages, code, err)
+ }
+ if len(messages.Items) != 2 || messages.Items[0].Message != "Parser started" || messages.Items[1].Message != "Parsing pages 2/8" {
+ t.Fatalf("messages = %+v, want lifecycle then latest progress", messages.Items)
+ }
+
+ log, code, err := NewDatasetService().GetIngestionLog(t.Context(), "kb-1", "user-1", "run-1")
+ if err != nil || code != common.CodeSuccess {
+ t.Fatalf("GetIngestionLog = (%+v, %v, %v), want success", log, code, err)
+ }
+ assertLatestEventMap(t, log, messages.Items[1].ID, "Parsing pages 2/8")
+}
+
+func insertMessageRun(t *testing.T, id, kbID, documentID string, status entity.TaskStatus, runCount int) {
+ t.Helper()
+ runCountPtr := &runCount
+ if err := dao.DB.Create(&entity.PipelineOperationLog{
+ ID: id,
+ DocumentID: documentID,
+ TenantID: "user-1",
+ KbID: kbID,
+ ParserID: "naive",
+ DocumentName: "doc.txt",
+ DocumentSuffix: ".txt",
+ DocumentType: "text",
+ SourceFrom: "local",
+ TaskType: string(entity.PipelineTaskTypeParse),
+ OperationStatus: string(status),
+ RunCount: runCountPtr,
+ }).Error; err != nil {
+ t.Fatalf("insert run: %v", err)
+ }
+}
+
+func insertMessageEvent(t *testing.T, db *gorm.DB, runID, taskID string, eventType int, message string) {
+ t.Helper()
+ if err := db.Model(&entity.IngestionTaskLog{}).Create(map[string]interface{}{
+ "task_id": taskID,
+ "pipeline_log_id": runID,
+ "checkpoint": entity.JSONMap{},
+ "event_type": eventType,
+ "component": "",
+ "phase": 0,
+ "message": message,
+ }).Error; err != nil {
+ t.Fatalf("insert event: %v", err)
+ }
+}
+
+func assertMessageEventIDs(t *testing.T, items []service.IngestionEventItem, want ...int) {
+ t.Helper()
+ if len(items) != len(want) {
+ t.Fatalf("item count = %d, want %d (%v)", len(items), len(want), want)
+ }
+ for i, item := range items {
+ if item.ID != want[i] {
+ t.Fatalf("item %d ID = %d, want %d", i, item.ID, want[i])
+ }
+ }
+}
+
+func assertLatestEventMap(t *testing.T, log map[string]interface{}, wantID int, wantMessage string) {
+ t.Helper()
+ event, ok := log["latest_ingestion_event"].(*service.IngestionEventItem)
+ if !ok || event == nil {
+ t.Fatalf("latest_ingestion_event = %#v, want event item", log["latest_ingestion_event"])
+ }
+ if event.ID != wantID {
+ t.Fatalf("latest event ID = %d, want %d", event.ID, wantID)
+ }
+ if event.Message != wantMessage {
+ t.Fatalf("latest event message = %q, want %q", event.Message, wantMessage)
+ }
+}
diff --git a/internal/service/document/document.go b/internal/service/document/document.go
index f90b5d39b7..e219e93c4e 100644
--- a/internal/service/document/document.go
+++ b/internal/service/document/document.go
@@ -35,7 +35,6 @@ type DocumentService struct {
kbDAO *dao.KnowledgebaseDAO
ingestionTaskDAO *dao.IngestionTaskDAO
ingestionTaskLogDAO *dao.IngestionTaskLogDAO
- pipelineLogDAO *dao.PipelineOperationLogDAO
ingestionTaskSvc *service.IngestionTaskService
docEngine engine.DocEngine
metadataSvc *service.MetadataService
@@ -44,6 +43,7 @@ type DocumentService struct {
fileDAO *dao.FileDAO
canvasDAO *dao.UserCanvasDAO
api4ConvDAO *dao.API4ConversationDAO
+ purgeTaskState func(context.Context, string) error
}
// NewDocumentService create document service
@@ -59,7 +59,6 @@ func NewDocumentService() *DocumentService {
documentDAO: dao.NewDocumentDAO(),
ingestionTaskDAO: dao.NewIngestionTaskDAO(),
ingestionTaskLogDAO: dao.NewIngestionTaskLogDAO(),
- pipelineLogDAO: dao.NewPipelineOperationLogDAO(),
ingestionTaskSvc: ingestionTaskSvc,
kbDAO: dao.NewKnowledgebaseDAO(),
docEngine: engine.Get(),
@@ -83,27 +82,28 @@ type UpdateDocumentRequest struct {
// DocumentResponse document response
type DocumentResponse struct {
- ID string `json:"id"`
- Name *string `json:"name,omitempty"`
- KbID string `json:"kb_id"`
- ParserID string `json:"parser_id"`
- PipelineID *string `json:"pipeline_id,omitempty"`
- Type string `json:"type"`
- SourceType string `json:"source_type"`
- CreatedBy string `json:"created_by"`
- Location *string `json:"location,omitempty"`
- Size int64 `json:"size"`
- TokenNum int64 `json:"token_num"`
- ChunkNum int64 `json:"chunk_num"`
- Progress float64 `json:"progress"`
- ProgressMsg *string `json:"progress_msg,omitempty"`
- ProcessBeginAt *time.Time `json:"process_begin_at,omitempty"`
- ProcessDuration float64 `json:"process_duration"`
- Suffix string `json:"suffix"`
- IngestionStatus string `json:"ingestion_status"`
- Status *string `json:"status,omitempty"`
- CreatedAt string `json:"created_at"`
- UpdatedAt string `json:"updated_at"`
+ ID string `json:"id"`
+ Name *string `json:"name,omitempty"`
+ KbID string `json:"kb_id"`
+ ParserID string `json:"parser_id"`
+ PipelineID *string `json:"pipeline_id,omitempty"`
+ Type string `json:"type"`
+ SourceType string `json:"source_type"`
+ CreatedBy string `json:"created_by"`
+ Location *string `json:"location,omitempty"`
+ Size int64 `json:"size"`
+ TokenNum int64 `json:"token_num"`
+ ChunkNum int64 `json:"chunk_num"`
+ Progress float64 `json:"progress"`
+ ProgressMsg *string `json:"progress_msg,omitempty"`
+ LatestIngestionEvent *service.IngestionEventItem `json:"latest_ingestion_event,omitempty"`
+ ProcessBeginAt *time.Time `json:"process_begin_at,omitempty"`
+ ProcessDuration float64 `json:"process_duration"`
+ Suffix string `json:"suffix"`
+ IngestionStatus string `json:"ingestion_status"`
+ Status *string `json:"status,omitempty"`
+ CreatedAt string `json:"created_at"`
+ UpdatedAt string `json:"updated_at"`
}
type ThumbnailResponse struct {
@@ -139,33 +139,34 @@ type UpdateDatasetDocumentRequest struct {
}
type UpdateDatasetDocumentResponse struct {
- ID string `json:"id"`
- Thumbnail *string `json:"thumbnail,omitempty"`
- DatasetID string `json:"dataset_id"`
- ParserID string `json:"parser_id"`
- PipelineID *string `json:"pipeline_id,omitempty"`
- ParserConfig map[string]interface{} `json:"parser_config"`
- SourceType string `json:"source_type"`
- Type string `json:"type"`
- CreatedBy string `json:"created_by"`
- Name *string `json:"name,omitempty"`
- Location *string `json:"location,omitempty"`
- Size int64 `json:"size"`
- TokenCount int64 `json:"token_count"`
- ChunkCount int64 `json:"chunk_count"`
- Progress float64 `json:"progress"`
- ProgressMsg *string `json:"progress_msg,omitempty"`
- ProcessBeginAt *time.Time `json:"process_begin_at,omitempty"`
- ProcessDuration float64 `json:"process_duration"`
- ContentHash *string `json:"content_hash,omitempty"`
- MetaFields map[string]interface{} `json:"meta_fields,omitempty"`
- Suffix string `json:"suffix"`
- IngestionStatus string `json:"ingestion_status"`
- Status *string `json:"status,omitempty"`
- CreateTime *int64 `json:"create_time,omitempty"`
- CreateDate *time.Time `json:"create_date,omitempty"`
- UpdateTime *int64 `json:"update_time,omitempty"`
- UpdateDate *time.Time `json:"update_date,omitempty"`
+ ID string `json:"id"`
+ Thumbnail *string `json:"thumbnail,omitempty"`
+ DatasetID string `json:"dataset_id"`
+ ParserID string `json:"parser_id"`
+ PipelineID *string `json:"pipeline_id,omitempty"`
+ ParserConfig map[string]interface{} `json:"parser_config"`
+ SourceType string `json:"source_type"`
+ Type string `json:"type"`
+ CreatedBy string `json:"created_by"`
+ Name *string `json:"name,omitempty"`
+ Location *string `json:"location,omitempty"`
+ Size int64 `json:"size"`
+ TokenCount int64 `json:"token_count"`
+ ChunkCount int64 `json:"chunk_count"`
+ Progress float64 `json:"progress"`
+ ProgressMsg *string `json:"progress_msg,omitempty"`
+ LatestIngestionEvent *service.IngestionEventItem `json:"latest_ingestion_event,omitempty"`
+ ProcessBeginAt *time.Time `json:"process_begin_at,omitempty"`
+ ProcessDuration float64 `json:"process_duration"`
+ ContentHash *string `json:"content_hash,omitempty"`
+ MetaFields map[string]interface{} `json:"meta_fields,omitempty"`
+ Suffix string `json:"suffix"`
+ IngestionStatus string `json:"ingestion_status"`
+ Status *string `json:"status,omitempty"`
+ CreateTime *int64 `json:"create_time,omitempty"`
+ CreateDate *time.Time `json:"create_date,omitempty"`
+ UpdateTime *int64 `json:"update_time,omitempty"`
+ UpdateDate *time.Time `json:"update_date,omitempty"`
}
var (
diff --git a/internal/service/document/document_crud.go b/internal/service/document/document_crud.go
index 2fb1a3d77c..d748199175 100644
--- a/internal/service/document/document_crud.go
+++ b/internal/service/document/document_crud.go
@@ -197,27 +197,6 @@ func (s *DocumentService) ApplyDocCounts(ctx context.Context, docID, kbID string
})
}
-// UpdateRunProgress mirrors a pipeline run's live progress into the document
-// row so the document-list endpoint (which reads document.progress/
-// progress_msg) reflects in-flight Go pipeline progress. Best-effort by
-// design; callers log and continue on error.
-func (s *DocumentService) UpdateRunProgress(ctx context.Context, docID string, progress float64, progressMsg string) error {
- updates := map[string]interface{}{
- "progress": progress,
- "progress_msg": progressMsg,
- }
- if doc, err := s.documentDAO.GetByID(ctx, dao.DB, docID); err != nil {
- return err
- } else if doc != nil && doc.ProcessBeginAt != nil {
- duration := time.Since(*doc.ProcessBeginAt).Seconds()
- if duration < 0 {
- duration = 0
- }
- updates["process_duration"] = duration
- }
- return s.documentDAO.UpdateByID(ctx, dao.DB, docID, updates)
-}
-
// UpdateRunState mirrors live progress into the document row when
// the existing progress log cannot be read. It intentionally leaves the log
// untouched so a later event can retry seeding and append it safely.
@@ -303,21 +282,23 @@ func (s *DocumentService) deleteDocumentFull(ctx context.Context, docID string)
return err
}
if ingestionTask != nil {
- taskInfo, err := s.ingestionTaskSvc.Remove(ctx, ingestionTask.ID, &ingestionTask.UserID)
- if err != nil {
+ if err := s.purgeTaskStateForCleanup(ctx, ingestionTask.ID); err != nil {
+ return err
+ }
+ if _, err := s.ingestionTaskSvc.Remove(ctx, ingestionTask.ID, &ingestionTask.UserID); err != nil {
return err
}
- // FIXME: need to add logic to delete files in taskInfo
- common.Warn(fmt.Sprintf("need to delete files from taskInfo: %v", taskInfo))
}
- s.deleteDocEngineData(ctx, docID, kb.TenantID, doc.KbID)
+ if err := s.deleteDocEngineData(ctx, docID, kb.TenantID, doc.KbID); err != nil {
+ return err
+ }
if err = s.deleteDocRecordWithCounters(ctx, doc, kb.ID); err != nil {
return err
}
- cleanupCtx := context.WithoutCancel(ctx)
- if err = s.cleanupFileReferences(cleanupCtx, docID); err != nil {
+ fileCleanupCtx := context.WithoutCancel(ctx)
+ if err = s.cleanupFileReferences(fileCleanupCtx, docID); err != nil {
return fmt.Errorf("document deleted but file cleanup failed: %w", err)
}
@@ -338,6 +319,18 @@ func (s *DocumentService) RemoveDocumentKeepFile(ctx context.Context, docID stri
if typeErr != nil {
common.Warn(fmt.Sprintf("RemoveDocumentKeepFile: failed to resolve knowledge compile types for %s: %v", docID, typeErr))
}
+ ingestionTask, err := s.ingestionTaskDAO.GetByDocumentID(ctx, dao.DB, docID)
+ if err != nil {
+ return fmt.Errorf("failed to get ingestion task for %s: %w", docID, err)
+ }
+ if ingestionTask != nil {
+ if err := s.purgeTaskStateForCleanup(ctx, ingestionTask.ID); err != nil {
+ return err
+ }
+ if _, err := s.ingestionTaskSvc.Remove(ctx, ingestionTask.ID, nil); err != nil {
+ return fmt.Errorf("remove ingestion task for document %s: %w", docID, err)
+ }
+ }
if _, delErr := s.taskDAO.DeleteByDocIDs(ctx, dao.DB, []string{docID}); delErr != nil {
if errors.Is(delErr, context.Canceled) || errors.Is(delErr, context.DeadlineExceeded) {
return fmt.Errorf("RemoveDocumentKeepFile: failed to delete tasks for %s: %w", docID, delErr)
@@ -417,17 +410,21 @@ func (s *DocumentService) resolveDocAndKB(ctx context.Context, docID string) (*e
// deleteDocEngineData removes chunks and metadata from the document engine.
// No-op when the engine is nil.
-func (s *DocumentService) deleteDocEngineData(ctx context.Context, docID, tenantID, kbID string) {
+func (s *DocumentService) deleteDocEngineData(ctx context.Context, docID, tenantID, kbID string) error {
if s.docEngine == nil {
- return
+ return nil
}
indexName := fmt.Sprintf("ragflow_%s", tenantID)
_, taskTypes, typeErr := s.documentKnowledgeCompileTypes(ctx, tenantID, kbID, docID)
if typeErr != nil {
common.Warn(fmt.Sprintf("deleteDocEngineData: failed to resolve knowledge compile types for %s: %v", docID, typeErr))
}
- if _, delErr := s.docEngine.DeleteChunks(ctx, map[string]interface{}{"doc_id": docID}, indexName, kbID); delErr != nil {
+ deleteCtx, cancel := context.WithTimeout(ctx, cleanupBatchTimeout)
+ _, delErr := s.docEngine.DeleteChunks(deleteCtx, map[string]interface{}{"doc_id": docID}, indexName, kbID)
+ cancel()
+ if delErr != nil {
common.Warn(fmt.Sprintf("deleteDocEngineData: failed to delete chunks for %s: %v", docID, delErr))
+ return fmt.Errorf("delete chunks for document %s: %w", docID, delErr)
}
// Notify the dataset-level post-processing consumer (§11) that this document's
// source + per-doc compiled chunks are gone. The consumer removes the
@@ -444,6 +441,7 @@ func (s *DocumentService) deleteDocEngineData(ctx context.Context, docID, tenant
if s.metadataSvc != nil {
_ = s.DeleteDocumentAllMetadata(ctx, docID) // logs internally
}
+ return nil
}
// deleteDocRecordWithCounters hard-deletes the document row and decrements the
diff --git a/internal/service/document/document_dataset_update.go b/internal/service/document/document_dataset_update.go
index f578df14c1..7fdc536f04 100644
--- a/internal/service/document/document_dataset_update.go
+++ b/internal/service/document/document_dataset_update.go
@@ -478,8 +478,10 @@ func (s *DocumentService) toUpdateDatasetDocumentResponse(ctx context.Context, d
metaFields = map[string]interface{}{}
}
ingestionStatus := "UNSTART"
+ var task *entity.IngestionTask
if s.ingestionTaskDAO != nil && doc != nil && doc.ID != "" {
- task, err := s.ingestionTaskDAO.GetByDocumentID(ctx, dao.DB, doc.ID)
+ var err error
+ task, err = s.ingestionTaskDAO.GetByDocumentID(ctx, dao.DB, doc.ID)
if err != nil {
return nil, fmt.Errorf("failed to get ingestion task for document %s: %w", doc.ID, err)
}
@@ -487,34 +489,40 @@ func (s *DocumentService) toUpdateDatasetDocumentResponse(ctx context.Context, d
ingestionStatus = task.Status
}
}
+ latestEventsByDocument, err := s.latestIngestionEventsByDocument(ctx, map[string]*entity.IngestionTask{doc.ID: task})
+ if err != nil {
+ common.Warn(fmt.Sprintf("get latest ingestion event for document %s: %v", doc.ID, err))
+ latestEventsByDocument = make(map[string]*service.IngestionEventItem)
+ }
return &UpdateDatasetDocumentResponse{
- ID: doc.ID,
- Thumbnail: doc.Thumbnail,
- DatasetID: doc.KbID,
- ParserID: doc.ParserID,
- PipelineID: doc.PipelineID,
- ParserConfig: doc.ParserConfig,
- SourceType: doc.SourceType,
- Type: doc.Type,
- CreatedBy: doc.CreatedBy,
- Name: doc.Name,
- Location: doc.Location,
- Size: doc.Size,
- TokenCount: doc.TokenNum,
- ChunkCount: doc.ChunkNum,
- Progress: doc.Progress,
- ProgressMsg: doc.ProgressMsg,
- ProcessBeginAt: doc.ProcessBeginAt,
- ProcessDuration: doc.ProcessDuration,
- ContentHash: doc.ContentHash,
- MetaFields: metaFields,
- Suffix: doc.Suffix,
- IngestionStatus: ingestionStatus,
- Status: doc.Status,
- CreateTime: doc.CreateTime,
- CreateDate: doc.CreateDate,
- UpdateTime: doc.UpdateTime,
- UpdateDate: doc.UpdateDate,
+ ID: doc.ID,
+ Thumbnail: doc.Thumbnail,
+ DatasetID: doc.KbID,
+ ParserID: doc.ParserID,
+ PipelineID: doc.PipelineID,
+ ParserConfig: doc.ParserConfig,
+ SourceType: doc.SourceType,
+ Type: doc.Type,
+ CreatedBy: doc.CreatedBy,
+ Name: doc.Name,
+ Location: doc.Location,
+ Size: doc.Size,
+ TokenCount: doc.TokenNum,
+ ChunkCount: doc.ChunkNum,
+ Progress: doc.Progress,
+ ProgressMsg: doc.ProgressMsg,
+ LatestIngestionEvent: latestEventsByDocument[doc.ID],
+ ProcessBeginAt: doc.ProcessBeginAt,
+ ProcessDuration: doc.ProcessDuration,
+ ContentHash: doc.ContentHash,
+ MetaFields: metaFields,
+ Suffix: doc.Suffix,
+ IngestionStatus: ingestionStatus,
+ Status: doc.Status,
+ CreateTime: doc.CreateTime,
+ CreateDate: doc.CreateDate,
+ UpdateTime: doc.UpdateTime,
+ UpdateDate: doc.UpdateDate,
}, nil
}
diff --git a/internal/service/document/document_ingest.go b/internal/service/document/document_ingest.go
index 81da5d28b9..157fd1e283 100644
--- a/internal/service/document/document_ingest.go
+++ b/internal/service/document/document_ingest.go
@@ -157,37 +157,8 @@ func (s *DocumentService) Ingest(ctx context.Context, userID string, req *Ingest
}
if req.Delete {
- // Capture the run's own log row before its task is removed. The
- // cleanup must drop only that row: a concurrent re-parse may
- // already own a newer one for the same document.
- var pipelineLogID string
- if task, taskErr := s.ingestionTaskDAO.GetByDocumentID(ctx, dao.DB, doc.ID); taskErr != nil {
- common.Error(fmt.Sprintf("go side, doc %s, load ingestion task for pipeline log cleanup failed", doc.ID), taskErr)
- } else if task != nil && task.PipelineLogID != nil {
- pipelineLogID = *task.PipelineLogID
- }
- if _, delErr := s.taskDAO.DeleteIngestionTasksByDocIDs(ctx, dao.DB, []string{doc.ID}); delErr != nil {
- if errors.Is(delErr, context.Canceled) || errors.Is(delErr, context.DeadlineExceeded) {
- return common.CodeExceptionError, fmt.Errorf("delete ingestion tasks: %w", delErr)
- }
- common.Error(fmt.Sprintf("go side, doc %s, DeleteIngestionTasksByDocIDs failed", doc.ID), delErr)
- } else if err := s.pipelineLogDAO.DeleteOpenLogByID(ctx, dao.DB, pipelineLogID); err != nil {
- common.Error(fmt.Sprintf("go side, doc %s, delete open pipeline log failed", doc.ID), err)
- }
- indexName := fmt.Sprintf("ragflow_%s", kb.TenantID)
- if s.docEngine != nil {
- var exists bool
- exists, err = s.docEngine.ChunkStoreExists(ctx, indexName, doc.KbID)
- if err != nil {
- common.Error(fmt.Sprintf("go side, doc %s, ChunkStoreExists failed", doc.ID), err)
- return common.CodeExceptionError, err
- }
- if exists {
- if _, err = s.docEngine.DeleteChunks(ctx, map[string]interface{}{"doc_id": doc.ID}, indexName, doc.KbID); err != nil {
- common.Error(fmt.Sprintf("go side, doc %s, DeleteChunks failed", doc.ID), err)
- return common.CodeExceptionError, err
- }
- }
+ if err := s.clearDocumentParseResults(ctx, doc, kb.TenantID); err != nil {
+ return common.CodeExceptionError, err
}
}
}
diff --git a/internal/service/document/document_list.go b/internal/service/document/document_list.go
index 52b163b5c2..dba2d7a1be 100644
--- a/internal/service/document/document_list.go
+++ b/internal/service/document/document_list.go
@@ -6,8 +6,10 @@ import (
"strings"
"time"
+ "ragflow/internal/common"
"ragflow/internal/dao"
"ragflow/internal/entity"
+ "ragflow/internal/service"
)
// escapeSQLLikePattern escapes the SQL LIKE wildcards ('%', '_') and
@@ -43,14 +45,21 @@ func (s *DocumentService) ListDocuments(ctx context.Context, page, pageSize int)
return nil, 0, fmt.Errorf("failed to get ingestion tasks for documents: %w", err)
}
}
+ latestEventsByDocument, err := s.latestIngestionEventsByDocument(ctx, taskMap)
+ if err != nil {
+ common.Warn(fmt.Sprintf("failed to get latest ingestion events for documents: %v", err))
+ latestEventsByDocument = make(map[string]*service.IngestionEventItem)
+ }
responses := make([]*DocumentResponse, len(documents))
for i, doc := range documents {
var task *entity.IngestionTask
+ var latestEvent *service.IngestionEventItem
if taskMap != nil && doc != nil {
task = taskMap[doc.ID]
+ latestEvent = latestEventsByDocument[doc.ID]
}
- responses[i] = s.toResponseWithTask(doc, task)
+ responses[i] = s.toResponseWithTask(doc, task, latestEvent)
}
return responses, total, nil
@@ -210,14 +219,21 @@ func (s *DocumentService) GetDocumentsByAuthorID(ctx context.Context, authorID,
return nil, 0, fmt.Errorf("failed to get ingestion tasks for documents: %w", err)
}
}
+ latestEventsByDocument, err := s.latestIngestionEventsByDocument(ctx, taskMap)
+ if err != nil {
+ common.Warn(fmt.Sprintf("failed to get latest ingestion events for documents: %v", err))
+ latestEventsByDocument = make(map[string]*service.IngestionEventItem)
+ }
responses := make([]*DocumentResponse, len(documents))
for i, doc := range documents {
var task *entity.IngestionTask
+ var latestEvent *service.IngestionEventItem
if taskMap != nil && doc != nil {
task = taskMap[doc.ID]
+ latestEvent = latestEventsByDocument[doc.ID]
}
- responses[i] = s.toResponseWithTask(doc, task)
+ responses[i] = s.toResponseWithTask(doc, task, latestEvent)
}
return responses, total, nil
@@ -226,16 +242,72 @@ func (s *DocumentService) GetDocumentsByAuthorID(ctx context.Context, authorID,
// toResponse convert model.Document to DocumentResponse
func (s *DocumentService) toResponse(ctx context.Context, doc *entity.Document) (*DocumentResponse, error) {
if s.ingestionTaskDAO == nil || doc == nil || doc.ID == "" {
- return s.toResponseWithTask(doc, nil), nil
+ return s.toResponseWithTask(doc, nil, nil), nil
}
task, err := s.ingestionTaskDAO.GetByDocumentID(ctx, dao.DB, doc.ID)
if err != nil {
return nil, fmt.Errorf("get ingestion task for document %s: %w", doc.ID, err)
}
- return s.toResponseWithTask(doc, task), nil
+ latestEventsByDocument, err := s.latestIngestionEventsByDocument(ctx, map[string]*entity.IngestionTask{doc.ID: task})
+ if err != nil {
+ common.Warn(fmt.Sprintf("get latest ingestion event for document %s: %v", doc.ID, err))
+ latestEventsByDocument = make(map[string]*service.IngestionEventItem)
+ }
+ return s.toResponseWithTask(doc, task, latestEventsByDocument[doc.ID]), nil
+}
+
+func (s *DocumentService) latestIngestionEventsByDocument(ctx context.Context, tasksByDocument map[string]*entity.IngestionTask) (map[string]*service.IngestionEventItem, error) {
+ result := make(map[string]*service.IngestionEventItem)
+ if len(tasksByDocument) == 0 {
+ return result, nil
+ }
+ runIDs := make([]string, 0, len(tasksByDocument))
+ for _, task := range tasksByDocument {
+ if task != nil && task.PipelineLogID != nil && *task.PipelineLogID != "" {
+ runIDs = append(runIDs, *task.PipelineLogID)
+ }
+ }
+ if len(runIDs) == 0 {
+ return result, nil
+ }
+ latestByRun, err := s.LatestIngestionEventsByPipelineLogIDs(ctx, runIDs)
+ if err != nil {
+ return nil, err
+ }
+ for documentID, task := range tasksByDocument {
+ if task == nil || task.PipelineLogID == nil {
+ continue
+ }
+ event := latestByRun[*task.PipelineLogID]
+ if event == nil {
+ continue
+ }
+ result[documentID] = event
+ }
+ return result, nil
+}
+
+// LatestIngestionEventsByPipelineLogIDs projects one real latest event for
+// every supplied run in a single batch query. Document list mappers receive
+// this data from their caller and never perform per-row event lookups.
+func (s *DocumentService) LatestIngestionEventsByPipelineLogIDs(ctx context.Context, pipelineLogIDs []string) (map[string]*service.IngestionEventItem, error) {
+ eventDAO := s.ingestionTaskLogDAO
+ if eventDAO == nil {
+ eventDAO = dao.NewIngestionTaskLogDAO()
+ }
+ latestByRun, err := eventDAO.LatestEventsByPipelineLogIDs(ctx, dao.DB, pipelineLogIDs)
+ if err != nil {
+ return nil, err
+ }
+ items := make(map[string]*service.IngestionEventItem, len(latestByRun))
+ for runID, event := range latestByRun {
+ item := service.IngestionEventItemFromLog(event)
+ items[runID] = &item
+ }
+ return items, nil
}
-func (s *DocumentService) toResponseWithTask(doc *entity.Document, task *entity.IngestionTask) *DocumentResponse {
+func (s *DocumentService) toResponseWithTask(doc *entity.Document, task *entity.IngestionTask, latestEvent *service.IngestionEventItem) *DocumentResponse {
if doc == nil {
return nil
}
@@ -265,26 +337,27 @@ func (s *DocumentService) toResponseWithTask(doc *entity.Document, task *entity.
ingestionStatus = task.Status
}
return &DocumentResponse{
- ID: doc.ID,
- Name: doc.Name,
- KbID: doc.KbID,
- ParserID: doc.ParserID,
- PipelineID: doc.PipelineID,
- Type: doc.Type,
- SourceType: doc.SourceType,
- CreatedBy: doc.CreatedBy,
- Location: doc.Location,
- Size: doc.Size,
- TokenNum: doc.TokenNum,
- ChunkNum: doc.ChunkNum,
- Progress: doc.Progress,
- ProgressMsg: doc.ProgressMsg,
- ProcessBeginAt: doc.ProcessBeginAt,
- ProcessDuration: doc.ProcessDuration,
- Suffix: doc.Suffix,
- IngestionStatus: ingestionStatus,
- Status: doc.Status,
- CreatedAt: createdAt,
- UpdatedAt: updatedAt,
+ ID: doc.ID,
+ Name: doc.Name,
+ KbID: doc.KbID,
+ ParserID: doc.ParserID,
+ PipelineID: doc.PipelineID,
+ Type: doc.Type,
+ SourceType: doc.SourceType,
+ CreatedBy: doc.CreatedBy,
+ Location: doc.Location,
+ Size: doc.Size,
+ TokenNum: doc.TokenNum,
+ ChunkNum: doc.ChunkNum,
+ Progress: doc.Progress,
+ ProgressMsg: doc.ProgressMsg,
+ LatestIngestionEvent: latestEvent,
+ ProcessBeginAt: doc.ProcessBeginAt,
+ ProcessDuration: doc.ProcessDuration,
+ Suffix: doc.Suffix,
+ IngestionStatus: ingestionStatus,
+ Status: doc.Status,
+ CreatedAt: createdAt,
+ UpdatedAt: updatedAt,
}
}
diff --git a/internal/service/document/document_parse.go b/internal/service/document/document_parse.go
index f1ef27757c..a3e91bd7dc 100644
--- a/internal/service/document/document_parse.go
+++ b/internal/service/document/document_parse.go
@@ -8,11 +8,14 @@ import (
"strconv"
"strings"
"sync"
+ "time"
"ragflow/internal/common"
"ragflow/internal/dao"
enginetypes "ragflow/internal/engine/types"
"ragflow/internal/entity"
+ "ragflow/internal/ingestion/knowledge_compile"
+ ingestionpipeline "ragflow/internal/ingestion/pipeline"
"ragflow/internal/storage"
"gorm.io/gorm"
@@ -29,6 +32,10 @@ var documentParseLocks = struct {
locks map[string]*documentParseLock
}{locks: make(map[string]*documentParseLock)}
+const (
+ cleanupBatchTimeout = 30 * time.Second
+)
+
func lockDocumentParse(docID string) func() {
documentParseLocks.Lock()
lock := documentParseLocks.locks[docID]
@@ -51,6 +58,20 @@ func lockDocumentParse(docID string) func() {
}
}
+// purgeTaskStateForCleanup removes resumable checkpoint, tracker, and chunk
+// cache state before a task row is deleted. The bounded context makes Redis
+// outages and hung clients observable to the caller instead of allowing a
+// destructive task/document delete to proceed with stale resume state.
+func (s *DocumentService) purgeTaskStateForCleanup(ctx context.Context, taskID string) error {
+ purgeTaskState := s.purgeTaskState
+ if purgeTaskState == nil {
+ purgeTaskState = ingestionpipeline.PurgeTaskState
+ }
+ batchCtx, cancel := context.WithTimeout(ctx, cleanupBatchTimeout)
+ defer cancel()
+ return purgeTaskState(batchCtx, taskID)
+}
+
// StartParseDocuments starts parsing a document via the DSL ingestion
// pipeline. It optionally clears prior results (RerunWithDelete), applies
// KB config (ApplyKB), validates storage, and enqueues an ingestion task.
@@ -124,8 +145,19 @@ func (s *DocumentService) clearDocumentParseResults(ctx context.Context, doc *en
if task.Status == common.RUNNING || task.Status == common.STOPPING {
return fmt.Errorf("document %s ingestion task is %s; stop it and wait for a terminal state before re-parsing", doc.ID, task.Status)
}
+ if task.Status == common.CREATED || task.Status == common.SCHEDULED {
+ if err := s.ingestionTaskSvc.SupersedeUnstartedTask(ctx, task.ID); err != nil {
+ return fmt.Errorf("supersede queued ingestion task for document %s: %w", doc.ID, err)
+ }
+ task.Status = common.STOPPED
+ }
taskExisted = true
}
+ if task != nil {
+ if err := s.purgeTaskStateForCleanup(ctx, task.ID); err != nil {
+ return fmt.Errorf("purge task state for document %s: %w", doc.ID, err)
+ }
+ }
// Delete terminal, CREATED, and SCHEDULED ingestion tasks atomically, leaving
// RUNNING/STOPPING tasks untouched so the check-then-delete window
@@ -138,15 +170,6 @@ func (s *DocumentService) clearDocumentParseResults(ctx context.Context, doc *en
if taskExisted && deleted == 0 {
return fmt.Errorf("document %s ingestion task started running; stop it before re-parsing", doc.ID)
}
- // The deleted CREATED/SCHEDULED task (if any) owned an open
- // pipeline-operation-log row that no worker will ever close. Drop that
- // run's row only: a concurrent re-parse may already own a newer one.
- if deleted > 0 && task != nil && task.PipelineLogID != nil {
- if err := s.pipelineLogDAO.DeleteOpenLogByID(ctx, dao.DB, *task.PipelineLogID); err != nil {
- common.Error(fmt.Sprintf("clear open pipeline log for document %s", doc.ID), err)
- }
- }
-
if err := s.clearDocumentAndKBCountersForRerun(doc.ID, doc.KbID); err != nil {
return err
}
@@ -163,9 +186,24 @@ func (s *DocumentService) clearDocumentParseResults(ctx context.Context, doc *en
if !exists {
return nil
}
+ _, taskTypes, err := s.documentKnowledgeCompileTypes(ctx, tenantID, doc.KbID, doc.ID)
+ if err != nil {
+ return fmt.Errorf("resolve generated products for document %s: %w", doc.ID, err)
+ }
+ if err := s.deleteDocumentGeneratedChunks(ctx, tenantID, doc.KbID, doc.ID); err != nil {
+ return fmt.Errorf("delete generated products for document %s: %w", doc.ID, err)
+ }
+ if err := s.deleteDocumentChunkImages(ctx, indexName, doc.KbID, doc.ID); err != nil {
+ return fmt.Errorf("delete chunk images for document %s: %w", doc.ID, err)
+ }
if err = s.deleteSourceChunks(ctx, tenantID, doc.KbID, doc.ID); err != nil {
return err
}
+ publishCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 15*time.Second)
+ defer cancel()
+ if err := knowledge_compile.PublishDeleted(publishCtx, tenantID, doc.KbID, doc.ID, taskTypes); err != nil {
+ return fmt.Errorf("publish document cleanup for %s: %w", doc.ID, err)
+ }
return nil
}
@@ -223,10 +261,6 @@ func (s *DocumentService) clearKBChunkNumWhenRerun(doc *entity.Document) error {
}
func (s *DocumentService) ParseDocuments(ctx context.Context, datasetID, userID string, docIDs []string) ([]*service.ParseDocumentResponse, error) {
- // create document parse id
- // save to task table
- // send to message queue
-
// deduplicate the document id
uniqueDocIDs := common.Deduplicate(docIDs)
if uniqueDocIDs == nil || len(uniqueDocIDs) == 0 {
@@ -264,26 +298,6 @@ func (s *DocumentService) ParseDocuments(ctx context.Context, datasetID, userID
continue
}
- // create task for each document
- //task := &entity.IngestionTask{
- // ID: utility.GenerateToken(),
- // DocumentID: docID,
- // UserID: userID,
- //}
-
- // save the task to database
- //err = s.ingestionTaskDAO.Create(task)
- //if err != nil {
- // errorMessage := err.Error()
- // responses = append(responses, &service.ParseDocumentResponse{
- // DocumentID: docID,
- // Result: &errorMessage,
- // })
- // continue
- //}
-
- // Send task to message queue
-
}
common.Info(fmt.Sprintf("parse documents, dataset: %s, documents: %v", datasetID, docIDs))
@@ -411,10 +425,8 @@ func (s *DocumentService) CancelDocParse(ctx context.Context, doc *entity.Docume
}
func (s *DocumentService) resetDocumentForReparse(ctx context.Context, doc *entity.Document, tenantID string, parserID *string, pipelineID *string) error {
- progressMsg := ""
updates := map[string]interface{}{
- "progress": 0,
- "progress_msg": progressMsg,
+ "progress": 0,
}
if parserID != nil {
updates["parser_id"] = *parserID
@@ -448,38 +460,59 @@ func (s *DocumentService) resetDocumentForReparse(ctx context.Context, doc *enti
}
func (s *DocumentService) deleteChunkImages(ctx context.Context, doc *entity.Document, indexName string) {
- if s.docEngine == nil {
+ if doc == nil {
return
}
+ _ = s.deleteDocumentChunkImages(ctx, indexName, doc.KbID, doc.ID)
+}
+
+// deleteDocumentChunkImages removes source chunk image objects in bounded
+// search batches. Repeating the operation is safe because deleting an absent
+// object is a no-op.
+func (s *DocumentService) deleteDocumentChunkImages(ctx context.Context, indexName, datasetID, documentID string) error {
+ if s.docEngine == nil {
+ return nil
+ }
storageImpl := storage.GetStorageFactory().GetStorage()
if storageImpl == nil {
- return
+ return nil
}
const pageSize = 1000
for offset := 0; ; offset += pageSize {
- result, err := s.docEngine.Search(ctx, &enginetypes.SearchRequest{
+ batchCtx, cancel := context.WithTimeout(ctx, cleanupBatchTimeout)
+ result, err := s.docEngine.Search(batchCtx, &enginetypes.SearchRequest{
IndexNames: []string{indexName},
- KbIDs: []string{doc.KbID},
+ KbIDs: []string{datasetID},
Offset: offset,
Limit: pageSize,
- SelectFields: []string{"id", "img_id"},
- Filter: map[string]interface{}{"doc_id": doc.ID},
- MatchExprs: nil,
- OrderBy: nil,
- RankFeature: nil,
+ SelectFields: []string{"id", "img_id", "compile_kwd"},
+ Filter: map[string]interface{}{"doc_id": documentID},
})
- if err != nil || result == nil || len(result.Chunks) == 0 {
- return
+ if err != nil {
+ cancel()
+ return err
+ }
+ if result == nil || len(result.Chunks) == 0 {
+ cancel()
+ return nil
}
for _, chunk := range result.Chunks {
- imageKey, ok := chunkImageStorageKey(doc.KbID, chunk)
- if !ok {
+ if strings.TrimSpace(documentStoreString(chunk["compile_kwd"])) != "" {
continue
}
- if storageImpl.ObjExist(ctx, doc.KbID, imageKey) {
- _ = storageImpl.Remove(ctx, doc.KbID, imageKey)
+ imageKey, ok := chunkImageStorageKey(datasetID, chunk)
+ if !ok || !storageImpl.ObjExist(batchCtx, datasetID, imageKey) {
+ continue
}
+ if err := storageImpl.Remove(batchCtx, datasetID, imageKey); err != nil {
+ cancel()
+ return err
+ }
+ }
+ cancel()
+ if int64(offset+len(result.Chunks)) >= result.Total {
+ return nil
}
}
}
diff --git a/internal/service/document/document_test.go b/internal/service/document/document_test.go
index f01b9b767c..f233be260b 100644
--- a/internal/service/document/document_test.go
+++ b/internal/service/document/document_test.go
@@ -237,6 +237,38 @@ type rerunDeleteDocEngine struct {
datasetID string
}
+type failingDeleteDocEngine struct {
+ fakeChatDocEngine
+ err error
+}
+
+type generatedCleanupDocEngine struct {
+ fakeChatDocEngine
+ deleteCalls int
+ generatedDeleted bool
+ generatedCond map[string]interface{}
+}
+
+func (e *generatedCleanupDocEngine) ChunkStoreExists(context.Context, string, string) (bool, error) {
+ return true, nil
+}
+
+func (e *generatedCleanupDocEngine) Search(_ context.Context, req *types.SearchRequest) (*types.SearchResult, error) {
+ if req.Offset > 0 {
+ return &types.SearchResult{Chunks: nil, Total: 1}, nil
+ }
+ return &types.SearchResult{Chunks: []map[string]interface{}{{"id": "source-1", "img_id": "kb-1-image-001"}}, Total: 1}, nil
+}
+
+func (e *generatedCleanupDocEngine) DeleteChunks(_ context.Context, condition map[string]interface{}, _, _ string) (int64, error) {
+ e.deleteCalls++
+ if available, ok := condition["available_int"].(int); ok && available == 0 {
+ e.generatedDeleted = true
+ e.generatedCond = condition
+ }
+ return 1, nil
+}
+
func (e *rerunDeleteDocEngine) ChunkStoreExists(context.Context, string, string) (bool, error) {
return true, nil
}
@@ -256,6 +288,10 @@ func (e *rerunDeleteDocEngine) DeleteChunks(_ context.Context, condition map[str
return 3, nil
}
+func (e *failingDeleteDocEngine) DeleteChunks(context.Context, map[string]interface{}, string, string) (int64, error) {
+ return 0, e.err
+}
+
type sourceAvailabilityDocEngine struct {
fakeChatDocEngine
updateConditions []map[string]interface{}
@@ -495,7 +531,6 @@ func testDocumentService(t *testing.T) *DocumentService {
file2DocumentDAO: dao.NewFile2DocumentDAO(),
fileDAO: dao.NewFileDAO(),
ingestionTaskDAO: dao.NewIngestionTaskDAO(),
- pipelineLogDAO: dao.NewPipelineOperationLogDAO(),
ingestionTaskSvc: service.NewIngestionTaskService(),
docEngine: nil,
metadataSvc: nil, // nil engine → metadata ops skipped
@@ -683,6 +718,72 @@ func TestDeleteDocumentFull_Basic(t *testing.T) {
}
}
+func TestDeleteDocumentFullPurgesTaskStateBeforeDeletingTask(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
+ insertTestIngestionTaskWithStatus(t, "task-1", "user-1", "doc-1", "kb-1", common.COMPLETED)
+
+ purgeErr := errors.New("redis unavailable")
+ svc := testDocumentService(t)
+ svc.purgeTaskState = func(context.Context, string) error { return purgeErr }
+
+ if err := svc.deleteDocumentFull(t.Context(), "doc-1"); !errors.Is(err, purgeErr) {
+ t.Fatalf("deleteDocumentFull error = %v, want purge error", err)
+ }
+ if task, err := svc.ingestionTaskDAO.GetByDocumentID(t.Context(), db, "doc-1"); err != nil {
+ t.Fatalf("reload ingestion task: %v", err)
+ } else if task == nil {
+ t.Fatal("ingestion task was deleted despite resumable state cleanup failure")
+ }
+ if _, err := svc.documentDAO.GetByID(t.Context(), db, "doc-1"); err != nil {
+ t.Fatalf("document was deleted despite resumable state cleanup failure: %v", err)
+ }
+}
+
+func TestDeleteDocumentFullKeepsDocumentWhenChunkDeletionFails(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
+
+ deleteErr := errors.New("chunk store unavailable")
+ svc := testDocumentService(t)
+ svc.docEngine = &failingDeleteDocEngine{err: deleteErr}
+
+ if err := svc.deleteDocumentFull(t.Context(), "doc-1"); !errors.Is(err, deleteErr) {
+ t.Fatalf("deleteDocumentFull error = %v, want chunk deletion error", err)
+ }
+ if _, err := svc.documentDAO.GetByID(t.Context(), db, "doc-1"); err != nil {
+ t.Fatalf("document was deleted after chunk deletion failure: %v", err)
+ }
+}
+
+func TestRemoveDocumentKeepFilePurgesTaskStateBeforeDeletingDocument(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
+ insertTestIngestionTaskWithStatus(t, "task-1", "user-1", "doc-1", "kb-1", common.COMPLETED)
+
+ purgeErr := errors.New("redis unavailable")
+ svc := testDocumentService(t)
+ svc.purgeTaskState = func(context.Context, string) error { return purgeErr }
+
+ if err := svc.RemoveDocumentKeepFile(t.Context(), "doc-1"); !errors.Is(err, purgeErr) {
+ t.Fatalf("RemoveDocumentKeepFile error = %v, want purge error", err)
+ }
+ if task, err := svc.ingestionTaskDAO.GetByDocumentID(t.Context(), db, "doc-1"); err != nil {
+ t.Fatalf("reload ingestion task: %v", err)
+ } else if task == nil {
+ t.Fatal("ingestion task was deleted despite resumable state cleanup failure")
+ }
+ if _, err := svc.documentDAO.GetByID(t.Context(), db, "doc-1"); err != nil {
+ t.Fatalf("document was deleted despite resumable state cleanup failure: %v", err)
+ }
+}
+
func TestDeleteDocumentFull_NotFound(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
@@ -1356,6 +1457,57 @@ func TestStartParseDocumentsSerializesConcurrentReruns(t *testing.T) {
}
}
+func TestStartParseDocumentsSupersedesQueuedRun(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 0, 10, 5)
+ insertTestDoc(t, "doc-1", "kb-1", 10, 5)
+ if err := db.Model(&entity.Document{}).Where("id = ?", "doc-1").Update("location", "loc-1").Error; err != nil {
+ t.Fatalf("set document location: %v", err)
+ }
+
+ svc := testDocumentService(t)
+ svc.ingestionTaskSvc.SetTaskPublisher(&recordingTaskPublisher{})
+ ctx := t.Context()
+ kb, err := svc.kbDAO.GetByID(ctx, db, "kb-1")
+ if err != nil {
+ t.Fatalf("load knowledgebase: %v", err)
+ }
+ doc, err := svc.documentDAO.GetByID(ctx, db, "doc-1")
+ if err != nil {
+ t.Fatalf("load document: %v", err)
+ }
+
+ if err = svc.StartParseDocuments(ctx, doc, kb, "user-1", StartParseOptions{}); err != nil {
+ t.Fatalf("start first parse: %v", err)
+ }
+ if err = svc.StartParseDocuments(ctx, doc, kb, "user-1", StartParseOptions{RerunWithDelete: true}); err != nil {
+ t.Fatalf("rerun queued parse: %v", err)
+ }
+
+ var logs []entity.PipelineOperationLog
+ if err = db.Where("document_id = ?", "doc-1").Order("run_count ASC").Find(&logs).Error; err != nil {
+ t.Fatalf("list pipeline logs: %v", err)
+ }
+ if len(logs) != 2 {
+ t.Fatalf("pipeline logs = %d, want 2", len(logs))
+ }
+ if logs[0].RunCount == nil || *logs[0].RunCount != 1 || logs[0].OperationStatus != string(entity.TaskStatusCancel) {
+ t.Fatalf("first run = %+v, want cancelled run 1", logs[0])
+ }
+ if logs[1].RunCount == nil || *logs[1].RunCount != 2 || logs[1].OperationStatus != string(entity.TaskStatusSchedule) {
+ t.Fatalf("second run = %+v, want scheduled run 2", logs[1])
+ }
+
+ var terminal entity.IngestionTaskLog
+ if err = db.Where("pipeline_log_id = ? AND event_type = ?", logs[0].ID, dao.EventTypeTerminal).First(&terminal).Error; err != nil {
+ t.Fatalf("load superseded terminal event: %v", err)
+ }
+ if terminal.Message != "Task superseded by a new parse request." {
+ t.Fatalf("terminal message = %q", terminal.Message)
+ }
+}
+
func TestStopParseDocuments_Success(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
@@ -2300,14 +2452,96 @@ func TestClearDocumentParseResultsClearsCountersTasksAndChunks(t *testing.T) {
if remainingTask != nil {
t.Fatalf("ingestion task should be deleted, status was %q", remainingTask.Status)
}
- if engine.deleteCalls != 1 {
- t.Fatalf("deleteCalls = %d, want 1", engine.deleteCalls)
+ if engine.deleteCalls != 2 {
+ t.Fatalf("deleteCalls = %d, want 2 (generated and source chunks)", engine.deleteCalls)
}
if engine.indexName != "ragflow_tenant-1" || engine.datasetID != "kb-1" || !reflect.DeepEqual(engine.condition["id"], []string{"source-1"}) {
t.Fatalf("unexpected delete call: index=%s dataset=%s condition=%v", engine.indexName, engine.datasetID, engine.condition)
}
}
+func TestClearDocumentParseResultsDeletesDocumentGeneratedChunks(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 0, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
+
+ engine := &generatedCleanupDocEngine{}
+ svc := testDocumentService(t)
+ svc.docEngine = engine
+ doc, err := svc.documentDAO.GetByID(t.Context(), db, "doc-1")
+ if err != nil {
+ t.Fatalf("load document: %v", err)
+ }
+
+ if err := svc.clearDocumentParseResults(t.Context(), doc, "tenant-1"); err != nil {
+ t.Fatalf("clearDocumentParseResults failed: %v", err)
+ }
+ if !engine.generatedDeleted {
+ t.Fatalf("document-generated chunks were not deleted; calls=%d", engine.deleteCalls)
+ }
+ if engine.generatedCond["doc_id"] != "doc-1" || engine.generatedCond["kb_id"] != "kb-1" {
+ t.Fatalf("generated cleanup condition = %#v, want document and dataset scope", engine.generatedCond)
+ }
+}
+
+func TestClearDocumentParseResultsDeletesSourceChunkImages(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 0, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
+
+ imageStore := newFakeUploadStorage()
+ if err := imageStore.Put(t.Context(), "kb-1", "image-001", []byte("png")); err != nil {
+ t.Fatalf("seed chunk image: %v", err)
+ }
+ factory := storage.GetStorageFactory()
+ previousStore := factory.GetStorage()
+ factory.SetStorage(imageStore)
+ t.Cleanup(func() { factory.SetStorage(previousStore) })
+
+ svc := testDocumentService(t)
+ svc.docEngine = &generatedCleanupDocEngine{}
+ doc, err := svc.documentDAO.GetByID(t.Context(), db, "doc-1")
+ if err != nil {
+ t.Fatalf("load document: %v", err)
+ }
+
+ if err := svc.clearDocumentParseResults(t.Context(), doc, "tenant-1"); err != nil {
+ t.Fatalf("clearDocumentParseResults failed: %v", err)
+ }
+ if imageStore.ObjExist(t.Context(), "kb-1", "image-001") {
+ t.Fatal("source chunk image still exists after cleanup")
+ }
+}
+
+func TestClearDocumentParseResultsPurgesTaskStateBeforeDeletingTask(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 0, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
+ insertTestIngestionTaskWithStatus(t, "task-1", "user-1", "doc-1", "kb-1", common.COMPLETED)
+
+ purgeErr := errors.New("redis unavailable")
+ svc := testDocumentService(t)
+ svc.purgeTaskState = func(context.Context, string) error { return purgeErr }
+ doc, err := svc.documentDAO.GetByID(t.Context(), db, "doc-1")
+ if err != nil {
+ t.Fatalf("load document: %v", err)
+ }
+
+ if err := svc.clearDocumentParseResults(t.Context(), doc, "tenant-1"); !errors.Is(err, purgeErr) {
+ t.Fatalf("clearDocumentParseResults error = %v, want purge error", err)
+ }
+ remaining, err := svc.ingestionTaskDAO.GetByDocumentID(t.Context(), db, "doc-1")
+ if err != nil {
+ t.Fatalf("reload ingestion task: %v", err)
+ }
+ if remaining == nil {
+ t.Fatal("task was deleted despite resumable state cleanup failure")
+ }
+}
+
func TestUpdateSourceChunkAvailabilityExcludesCompiledProducts(t *testing.T) {
docEngine := &sourceAvailabilityDocEngine{}
svc := testDocumentService(t)
@@ -3841,32 +4075,55 @@ func TestIngest_DeleteOnlyCleansTasks(t *testing.T) {
}
}
-func TestUpdateRunProgressMirrorsFields(t *testing.T) {
+func TestDocumentResponseEmbedsLatestEventForCurrentRun(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
insertTestDoc(t, "doc-1", "kb-1", 0, 0)
- begin := time.Now().Add(-2 * time.Second)
- if err := db.Model(&entity.Document{}).Where("id = ?", "doc-1").Update("process_begin_at", begin).Error; err != nil {
- t.Fatalf("set process_begin_at: %v", err)
+ insertTestIngestionTask(t, "task-1", "user-1", "doc-1", "kb-1")
+ runCount := 1
+ if err := db.Create(&entity.PipelineOperationLog{
+ ID: "run-1",
+ DocumentID: "doc-1",
+ TenantID: "tenant-1",
+ KbID: "kb-1",
+ ParserID: "naive",
+ DocumentName: "doc.txt",
+ DocumentSuffix: ".txt",
+ DocumentType: "text",
+ SourceFrom: "local",
+ TaskType: string(entity.PipelineTaskTypeParse),
+ OperationStatus: string(entity.TaskStatusRunning),
+ RunCount: &runCount,
+ }).Error; err != nil {
+ t.Fatalf("insert run: %v", err)
+ }
+ if err := db.Model(&entity.IngestionTask{}).Where("id = ?", "task-1").Update("pipeline_log_id", "run-1").Error; err != nil {
+ t.Fatalf("bind run: %v", err)
+ }
+ for _, message := range []string{"first", "latest"} {
+ if err := db.Model(&entity.IngestionTaskLog{}).Create(map[string]interface{}{
+ "task_id": "task-1",
+ "pipeline_log_id": "run-1",
+ "checkpoint": entity.JSONMap{},
+ "event_type": dao.EventTypeMessage,
+ "component": "",
+ "phase": 0,
+ "message": message,
+ }).Error; err != nil {
+ t.Fatalf("insert event: %v", err)
+ }
}
- svc := testDocumentService(t)
- ctx := t.Context()
- if err := svc.UpdateRunProgress(ctx, "doc-1", 0.5, "halfway"); err != nil {
- t.Fatalf("UpdateRunProgress failed: %v", err)
- }
- doc, err := dao.NewDocumentDAO().GetByID(ctx, db, "doc-1")
+ doc, err := dao.NewDocumentDAO().GetByID(t.Context(), db, "doc-1")
if err != nil {
t.Fatalf("load document: %v", err)
}
- if doc.Progress != 0.5 {
- t.Fatalf("progress = %v, want 0.5", doc.Progress)
- }
- if doc.ProgressMsg == nil || *doc.ProgressMsg != "halfway" {
- t.Fatalf("progress_msg = %v, want halfway", doc.ProgressMsg)
+ response, err := testDocumentService(t).toResponse(t.Context(), doc)
+ if err != nil {
+ t.Fatalf("toResponse: %v", err)
}
- if doc.ProcessDuration <= 0 {
- t.Fatalf("process_duration = %v, want positive live duration", doc.ProcessDuration)
+ if response.LatestIngestionEvent == nil || response.LatestIngestionEvent.ID != 2 || response.LatestIngestionEvent.Message != "latest" {
+ t.Fatalf("response = %+v, want latest current-run event", response)
}
}
diff --git a/internal/service/document/knowledge_compile_status.go b/internal/service/document/knowledge_compile_status.go
index 24f39c8684..049ed5d884 100644
--- a/internal/service/document/knowledge_compile_status.go
+++ b/internal/service/document/knowledge_compile_status.go
@@ -69,7 +69,8 @@ func (s *DocumentService) documentKnowledgeCompileTypes(ctx context.Context, ten
seenVariants := make(map[string]struct{})
seenTaskTypes := make(map[string]struct{})
for offset := 0; ; offset += 1000 {
- result, err := s.docEngine.Search(ctx, &types.SearchRequest{
+ searchCtx, cancel := context.WithTimeout(ctx, cleanupBatchTimeout)
+ result, err := s.docEngine.Search(searchCtx, &types.SearchRequest{
IndexNames: []string{indexName},
KbIDs: []string{datasetID},
Offset: offset,
@@ -77,6 +78,7 @@ func (s *DocumentService) documentKnowledgeCompileTypes(ctx context.Context, ten
SelectFields: []string{"compile_kwd", "compilation_template_kind_kwd"},
Filter: map[string]any{"doc_id": []string{documentID}},
})
+ cancel()
if err != nil {
return nil, nil, err
}
diff --git a/internal/service/document/wiki_refresh.go b/internal/service/document/wiki_refresh.go
index 1e27a034fa..06805e8f46 100644
--- a/internal/service/document/wiki_refresh.go
+++ b/internal/service/document/wiki_refresh.go
@@ -59,7 +59,8 @@ func (s *DocumentService) updateSourceChunkAvailability(ctx context.Context, ten
func (s *DocumentService) loadSourceChunkIDs(ctx context.Context, indexName, datasetID, documentID string) ([]string, error) {
ids := make([]string, 0)
for offset := 0; ; offset += sourceChunkAvailabilityBatchSize {
- result, err := s.docEngine.Search(ctx, &enginetypes.SearchRequest{
+ searchCtx, cancel := context.WithTimeout(ctx, cleanupBatchTimeout)
+ result, err := s.docEngine.Search(searchCtx, &enginetypes.SearchRequest{
IndexNames: []string{indexName},
KbIDs: []string{datasetID},
Offset: offset,
@@ -67,6 +68,7 @@ func (s *DocumentService) loadSourceChunkIDs(ctx context.Context, indexName, dat
SelectFields: []string{"id", "compile_kwd"},
Filter: map[string]any{"doc_id": []string{documentID}},
})
+ cancel()
if err != nil {
return nil, err
}
@@ -99,16 +101,35 @@ func (s *DocumentService) deleteSourceChunks(ctx context.Context, tenantID, data
}
for start := 0; start < len(ids); start += sourceChunkAvailabilityBatchSize {
end := min(start+sourceChunkAvailabilityBatchSize, len(ids))
- if _, err := s.docEngine.DeleteChunks(ctx, map[string]any{
+ batchCtx, cancel := context.WithTimeout(ctx, cleanupBatchTimeout)
+ _, err = s.docEngine.DeleteChunks(batchCtx, map[string]any{
"id": ids[start:end],
"kb_id": datasetID,
- }, indexName, datasetID); err != nil {
+ }, indexName, datasetID)
+ cancel()
+ if err != nil {
return err
}
}
return nil
}
+// deleteDocumentGeneratedChunks removes document-scoped compiler products
+// without touching source chunks or dataset-level merged products.
+func (s *DocumentService) deleteDocumentGeneratedChunks(ctx context.Context, tenantID, datasetID, documentID string) error {
+ if s.docEngine == nil {
+ return nil
+ }
+ batchCtx, cancel := context.WithTimeout(ctx, cleanupBatchTimeout)
+ _, err := s.docEngine.DeleteChunks(batchCtx, map[string]any{
+ "doc_id": documentID,
+ "kb_id": datasetID,
+ "available_int": 0,
+ }, fmt.Sprintf("ragflow_%s", tenantID), datasetID)
+ cancel()
+ return err
+}
+
func (s *DocumentService) markDocumentWikiDirty(ctx context.Context, tenantID, datasetID, documentID string) {
markCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 15*time.Second)
defer cancel()
diff --git a/internal/service/ingestion_event.go b/internal/service/ingestion_event.go
new file mode 100644
index 0000000000..25546f0c0d
--- /dev/null
+++ b/internal/service/ingestion_event.go
@@ -0,0 +1,33 @@
+package service
+
+import (
+ "time"
+
+ "ragflow/internal/entity"
+)
+
+// IngestionEventItem is the API representation of one persisted ingestion
+// event. It is deliberately a one-to-one projection: callers must not join or
+// rewrite messages into a synthetic status string.
+type IngestionEventItem struct {
+ ID int `json:"id"`
+ TS *time.Time `json:"ts"`
+ EventType int `json:"event_type"`
+ Component string `json:"component"`
+ Phase int `json:"phase"`
+ Message string `json:"message"`
+}
+
+func IngestionEventItemFromLog(event *entity.IngestionTaskLog) IngestionEventItem {
+ if event == nil {
+ return IngestionEventItem{}
+ }
+ return IngestionEventItem{
+ ID: event.ID,
+ TS: event.CreateDate,
+ EventType: event.EventType,
+ Component: event.Component,
+ Phase: event.Phase,
+ Message: event.Message,
+ }
+}
diff --git a/internal/service/ingestion_log_retention.go b/internal/service/ingestion_log_retention.go
new file mode 100644
index 0000000000..950c003de0
--- /dev/null
+++ b/internal/service/ingestion_log_retention.go
@@ -0,0 +1,354 @@
+package service
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "time"
+
+ "ragflow/internal/common"
+ "ragflow/internal/dao"
+ "ragflow/internal/entity"
+
+ "go.uber.org/zap"
+ "gorm.io/gorm"
+)
+
+type ingestionLogFoldResult struct {
+ Before int
+ Protected int
+ DeletedRows int
+ OmittedEvents int
+ After int
+ SummaryID int
+ Skipped bool
+ SkipReason string
+}
+
+type ingestionDocumentTrimResult struct {
+ BeforeEvents int
+ DeletedRuns int
+ DeletedEvents int
+ AfterEvents int
+}
+
+func logIngestionFoldResult(taskID, pipelineLogID string, result ingestionLogFoldResult, duration time.Duration, err error) {
+ fields := []zap.Field{
+ zap.String("event", "ingestion_log_fold"),
+ zap.String("task_id", taskID),
+ zap.String("pipeline_log_id", pipelineLogID),
+ zap.Int("before", result.Before),
+ zap.Int("after", result.After),
+ zap.Int("deleted", result.DeletedRows),
+ zap.Int("omitted", result.OmittedEvents),
+ zap.Int("protected", result.Protected),
+ zap.Int("summary_id", result.SummaryID),
+ zap.Duration("duration", duration),
+ }
+ if err != nil {
+ fields = append(fields, zap.String("result", "failure"))
+ common.Error("ingestion log fold failed", err, fields...)
+ return
+ }
+ if result.Skipped {
+ fields = append(fields, zap.String("result", "skipped"), zap.String("reason", result.SkipReason))
+ common.Info("ingestion log fold skipped", fields...)
+ return
+ }
+ fields = append(fields, zap.String("result", "success"))
+ common.Info("ingestion log fold completed", fields...)
+}
+
+func logIngestionTrimResult(documentID, taskID, preservePipelineLogID string, result ingestionDocumentTrimResult, duration time.Duration, err error) {
+ fields := []zap.Field{
+ zap.String("event", "ingestion_log_run_delete"),
+ zap.String("document_id", documentID),
+ zap.String("task_id", taskID),
+ zap.String("preserve_pipeline_log_id", preservePipelineLogID),
+ zap.Int("before", result.BeforeEvents),
+ zap.Int("after", result.AfterEvents),
+ zap.Int("deleted_runs", result.DeletedRuns),
+ zap.Int("deleted_events", result.DeletedEvents),
+ zap.Duration("duration", duration),
+ }
+ if err != nil {
+ fields = append(fields, zap.String("result", "failure"))
+ common.Error("ingestion log run deletion failed", err, fields...)
+ return
+ }
+ fields = append(fields, zap.String("result", "success"), zap.String("reason", "document_event_cap"))
+ common.Info("ingestion log run deletion completed", fields...)
+}
+
+// foldIngestionRun compacts one terminal run without changing the run's
+// lifecycle truth. The function is intentionally private: callers must first
+// settle the run and then invoke this same implementation, rather than
+// maintaining a second cleanup path.
+func (s *IngestionTaskService) foldIngestionRun(ctx context.Context, pipelineLogID string, maxRows int) (ingestionLogFoldResult, error) {
+ if pipelineLogID == "" {
+ return ingestionLogFoldResult{}, errors.New("fold ingestion run requires a pipeline log id")
+ }
+ if maxRows < 2 {
+ return ingestionLogFoldResult{}, errors.New("fold ingestion run requires max rows >= 2")
+ }
+
+ var result ingestionLogFoldResult
+ err := dao.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ var run entity.PipelineOperationLog
+ if err := tx.WithContext(ctx).Where("id = ?", pipelineLogID).First(&run).Error; err != nil {
+ return err
+ }
+
+ var events []*entity.IngestionTaskLog
+ if err := tx.WithContext(ctx).
+ Where("pipeline_log_id = ?", pipelineLogID).
+ Order("id ASC").Find(&events).Error; err != nil {
+ return err
+ }
+ result.Before = len(events)
+ result.After = result.Before
+
+ var latestTerminal *entity.IngestionTaskLog
+ var latestSummary *entity.IngestionTaskLog
+ latestLifecycle := make(map[string]*entity.IngestionTaskLog)
+ for _, event := range events {
+ switch event.EventType {
+ case dao.EventTypeLifecycle:
+ if event.Component != "" {
+ latestLifecycle[event.Component] = event
+ }
+ case dao.EventTypeTerminal:
+ latestTerminal = event
+ case dao.EventTypeSystem:
+ if latestSummary == nil || event.ID > latestSummary.ID {
+ latestSummary = event
+ }
+ }
+ }
+
+ if latestSummary != nil {
+ result.SummaryID = latestSummary.ID
+ }
+ if !isTerminalPipelineOperationStatus(run.OperationStatus) {
+ result.Skipped = true
+ result.SkipReason = "run_not_terminal"
+ return nil
+ }
+ if result.Before <= maxRows {
+ return nil
+ }
+
+ protected := make(map[int]struct{}, len(latestLifecycle)+2)
+ for _, event := range latestLifecycle {
+ protected[event.ID] = struct{}{}
+ }
+ if latestTerminal != nil {
+ protected[latestTerminal.ID] = struct{}{}
+ }
+ if latestSummary != nil {
+ protected[latestSummary.ID] = struct{}{}
+ }
+ result.Protected = len(protected)
+ if result.Protected >= maxRows {
+ result.Skipped = true
+ result.SkipReason = "protected_rows_reach_limit"
+ return nil
+ }
+
+ ordinary := make([]*entity.IngestionTaskLog, 0, len(events)-len(protected))
+ for _, event := range events {
+ if _, keep := protected[event.ID]; !keep {
+ ordinary = append(ordinary, event)
+ }
+ }
+
+ summaryReserved := 0
+ if latestSummary == nil {
+ summaryReserved = 1
+ }
+ ordinaryBudget := maxRows - result.Protected - summaryReserved
+ if ordinaryBudget < 0 {
+ result.Skipped = true
+ result.SkipReason = "protected_rows_reach_limit"
+ return nil
+ }
+
+ // Keep the newest ordinary messages first, then use the remaining
+ // budget for the oldest ordinary messages. Dynamic 4/5 tail and 1/5
+ // head allocation scales to any configured cap while preserving the
+ // 4,000 / 999 balance under the default 5,000-row cap.
+ tailBudget := (ordinaryBudget*4 + 4) / 5
+ if tailBudget > ordinaryBudget {
+ tailBudget = ordinaryBudget
+ }
+ headBudget := ordinaryBudget - tailBudget
+ keepOrdinary := make(map[int]struct{}, headBudget+tailBudget)
+ for _, event := range ordinary[:headBudget] {
+ keepOrdinary[event.ID] = struct{}{}
+ }
+ for _, event := range ordinary[len(ordinary)-tailBudget:] {
+ keepOrdinary[event.ID] = struct{}{}
+ }
+
+ deleteEvents := make([]*entity.IngestionTaskLog, 0, len(ordinary))
+ for _, event := range ordinary {
+ if _, keep := keepOrdinary[event.ID]; !keep {
+ deleteEvents = append(deleteEvents, event)
+ }
+ }
+ if len(deleteEvents) == 0 {
+ return nil
+ }
+
+ newSummary := latestSummary == nil
+ if newSummary {
+ latestSummary = deleteEvents[0]
+ result.SummaryID = latestSummary.ID
+ deleteEvents = deleteEvents[1:]
+ }
+ omittedEvents := len(deleteEvents) + boolToInt(newSummary)
+ omittedRows := deleteEvents
+ if newSummary {
+ omittedRows = append([]*entity.IngestionTaskLog{latestSummary}, deleteEvents...)
+ }
+ summaryMessage := formatIngestionFoldMessage(omittedEvents, omittedRows, latestSummary)
+ if err := tx.WithContext(ctx).Model(&entity.IngestionTaskLog{}).
+ Where("id = ?", latestSummary.ID).
+ Updates(map[string]interface{}{
+ "checkpoint": entity.JSONMap{},
+ "event_type": dao.EventTypeSystem,
+ "component": "",
+ "phase": 0,
+ "message": summaryMessage,
+ }).Error; err != nil {
+ return err
+ }
+
+ deleteIDs := make([]int, 0, len(deleteEvents))
+ for _, event := range deleteEvents {
+ deleteIDs = append(deleteIDs, event.ID)
+ }
+ if len(deleteIDs) > 0 {
+ if err := tx.WithContext(ctx).Where("id IN ?", deleteIDs).Delete(&entity.IngestionTaskLog{}).Error; err != nil {
+ return err
+ }
+ }
+ result.DeletedRows = len(deleteIDs)
+ result.OmittedEvents = omittedEvents
+
+ var after int64
+ if err := tx.WithContext(ctx).Model(&entity.IngestionTaskLog{}).
+ Where("pipeline_log_id = ?", pipelineLogID).Count(&after).Error; err != nil {
+ return err
+ }
+ result.After = int(after)
+ return nil
+ })
+ return result, err
+}
+
+// trimIngestionDocument enforces the cross-run event cap by deleting complete
+// oldest terminal runs. It deliberately leaves pipeline_operation_log rows in
+// place: run_count is the document's immutable history ledger. The latest run
+// and the caller's run are never deleted, even if a late terminal writer is
+// settling an older delivery.
+func (s *IngestionTaskService) trimIngestionDocument(ctx context.Context, documentID, preservePipelineLogID string, maxRows int) (ingestionDocumentTrimResult, error) {
+ if documentID == "" {
+ return ingestionDocumentTrimResult{}, errors.New("trim ingestion document requires a document id")
+ }
+ if maxRows <= 0 {
+ return ingestionDocumentTrimResult{}, errors.New("trim ingestion document requires positive max rows")
+ }
+
+ var result ingestionDocumentTrimResult
+ err := dao.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ var runs []*entity.PipelineOperationLog
+ if err := tx.WithContext(ctx).
+ Where("document_id = ? AND run_count IS NOT NULL AND run_count > 0", documentID).
+ Order("run_count ASC").Order("create_time ASC").Order("id ASC").
+ Find(&runs).Error; err != nil {
+ return err
+ }
+ if len(runs) == 0 {
+ return nil
+ }
+
+ latestRunID := runs[len(runs)-1].ID
+ preserved := map[string]struct{}{latestRunID: {}}
+ if preservePipelineLogID != "" {
+ preserved[preservePipelineLogID] = struct{}{}
+ }
+
+ eventCounts := make(map[string]int, len(runs))
+ for _, run := range runs {
+ var count int64
+ if err := tx.WithContext(ctx).Model(&entity.IngestionTaskLog{}).
+ Where("pipeline_log_id = ?", run.ID).Count(&count).Error; err != nil {
+ return err
+ }
+ eventCounts[run.ID] = int(count)
+ result.BeforeEvents += int(count)
+ }
+ result.AfterEvents = result.BeforeEvents
+
+ for _, run := range runs {
+ if result.AfterEvents <= maxRows {
+ break
+ }
+ if _, keep := preserved[run.ID]; keep || !isTerminalPipelineOperationStatus(run.OperationStatus) {
+ continue
+ }
+ count := eventCounts[run.ID]
+ if count == 0 {
+ continue
+ }
+ if err := tx.WithContext(ctx).Where("pipeline_log_id = ?", run.ID).Delete(&entity.IngestionTaskLog{}).Error; err != nil {
+ return err
+ }
+ result.DeletedRuns++
+ result.DeletedEvents += count
+ result.AfterEvents -= count
+ }
+ return nil
+ })
+ return result, err
+}
+
+func isTerminalPipelineOperationStatus(status string) bool {
+ switch status {
+ case string(entity.TaskStatusDone), string(entity.TaskStatusFail), string(entity.TaskStatusCancel), "DONE", "FAIL", "CANCEL":
+ return true
+ default:
+ return false
+ }
+}
+
+func formatIngestionFoldMessage(omitted int, omittedRows []*entity.IngestionTaskLog, summary *entity.IngestionTaskLog) string {
+ first := summary
+ last := summary
+ if len(omittedRows) > 0 {
+ first = omittedRows[0]
+ last = omittedRows[len(omittedRows)-1]
+ }
+ return fmt.Sprintf("… this run omitted %d process messages (time range %s–%s) …", omitted, foldEventTime(first), foldEventTime(last))
+}
+
+func foldEventTime(event *entity.IngestionTaskLog) string {
+ if event == nil {
+ return "unknown"
+ }
+ if event.CreateDate != nil {
+ return event.CreateDate.Local().Format("15:04:05")
+ }
+ if event.CreateTime != nil {
+ return time.UnixMilli(*event.CreateTime).Local().Format("15:04:05")
+ }
+ return "unknown"
+}
+
+func boolToInt(value bool) int {
+ if value {
+ return 1
+ }
+ return 0
+}
diff --git a/internal/service/ingestion_log_retention_test.go b/internal/service/ingestion_log_retention_test.go
new file mode 100644
index 0000000000..e7a27f827e
--- /dev/null
+++ b/internal/service/ingestion_log_retention_test.go
@@ -0,0 +1,342 @@
+package service
+
+import (
+ "fmt"
+ "strings"
+ "testing"
+
+ "ragflow/internal/dao"
+ "ragflow/internal/entity"
+
+ "gorm.io/gorm"
+)
+
+func TestFoldIngestionRunLeavesRunningRunUntouched(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertFoldPipelineLog(t, db, "run-1", entity.TaskStatusRunning, 1)
+ for i := 0; i < 6; i++ {
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, 0, "", "detail")
+ }
+
+ result, err := NewIngestionTaskService().foldIngestionRun(t.Context(), "run-1", 4)
+ if err != nil {
+ t.Fatalf("foldIngestionRun failed: %v", err)
+ }
+ if !result.Skipped || result.SkipReason != "run_not_terminal" {
+ t.Fatalf("result = %+v, want running run skipped", result)
+ }
+ if got := countFoldEvents(t, db, "run-1"); got != 6 {
+ t.Fatalf("event count = %d, want 6", got)
+ }
+}
+
+func TestFoldIngestionRunProtectsLatestLifecycleAndTerminal(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertFoldPipelineLog(t, db, "run-1", entity.TaskStatusDone, 1)
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeLifecycle, 0, "Parser", "parser started")
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, 0, "", "head detail")
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeLifecycle, 1, "Parser", "parser done")
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeLifecycle, 0, "Chunker", "chunker started")
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, 0, "", "middle detail 1")
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, 0, "", "middle detail 2")
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeTerminal, 0, "", "Task completed.")
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, 0, "", "tail detail")
+
+ result, err := NewIngestionTaskService().foldIngestionRun(t.Context(), "run-1", 5)
+ if err != nil {
+ t.Fatalf("foldIngestionRun failed: %v", err)
+ }
+ if result.Before != 8 || result.After != 5 || result.Protected != 3 || result.DeletedRows != 3 || result.SummaryID == 0 {
+ t.Fatalf("result = %+v, want 8 -> 5 with 3 protected/deleted rows and summary", result)
+ }
+ if got := countFoldEvents(t, db, "run-1"); got != 5 {
+ t.Fatalf("event count = %d, want 5", got)
+ }
+
+ events := listFoldEvents(t, db, "run-1")
+ var parserDone, chunkerStarted, terminal, summary, tail bool
+ for _, event := range events {
+ switch {
+ case event.EventType == dao.EventTypeLifecycle && event.Component == "Parser" && event.Phase == 1:
+ parserDone = true
+ case event.EventType == dao.EventTypeLifecycle && event.Component == "Chunker" && event.Phase == 0:
+ chunkerStarted = true
+ case event.EventType == dao.EventTypeTerminal:
+ terminal = true
+ case event.EventType == dao.EventTypeSystem:
+ summary = event.Component == "" && event.Phase == 0 && event.Message != ""
+ case event.EventType == dao.EventTypeMessage && event.Message == "tail detail":
+ tail = true
+ }
+ }
+ if !parserDone || !chunkerStarted || !terminal || !summary || !tail {
+ t.Fatalf("folded events lost protected/latest rows: %+v", events)
+ }
+ if !containsFoldMessage(events, "omitted 4 process messages") {
+ t.Fatalf("fold summary does not report four omitted events: %+v", events)
+ }
+
+ progress, err := dao.NewIngestionTaskLogDAO().AggregateProgressByPipelineLogID(t.Context(), db, "run-1", 2)
+ if err != nil {
+ t.Fatalf("aggregate folded progress: %v", err)
+ }
+ if progress.Done != 1 || progress.Running != 1 || progress.Failed != 0 {
+ t.Fatalf("progress after fold = %+v, want parser done/chunker running", progress)
+ }
+}
+
+func containsFoldMessage(events []*entity.IngestionTaskLog, fragment string) bool {
+ for _, event := range events {
+ if event.EventType == dao.EventTypeSystem && strings.Contains(event.Message, fragment) {
+ return true
+ }
+ }
+ return false
+}
+
+func TestFoldIngestionRunIsIdempotent(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertFoldPipelineLog(t, db, "run-1", entity.TaskStatusFail, 1)
+ for i := 0; i < 8; i++ {
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, 0, "", "detail")
+ }
+
+ first, err := NewIngestionTaskService().foldIngestionRun(t.Context(), "run-1", 5)
+ if err != nil {
+ t.Fatalf("first fold failed: %v", err)
+ }
+ second, err := NewIngestionTaskService().foldIngestionRun(t.Context(), "run-1", 5)
+ if err != nil {
+ t.Fatalf("second fold failed: %v", err)
+ }
+ if first.SummaryID == 0 || second.SummaryID != first.SummaryID {
+ t.Fatalf("summary ids = %d then %d, want the same row", first.SummaryID, second.SummaryID)
+ }
+ if second.DeletedRows != 0 || second.Before != second.After {
+ t.Fatalf("second result = %+v, want idempotent no-op", second)
+ }
+ var summaries int64
+ if err := db.Model(&entity.IngestionTaskLog{}).
+ Where("pipeline_log_id = ? AND event_type = ?", "run-1", dao.EventTypeSystem).
+ Count(&summaries).Error; err != nil {
+ t.Fatalf("count summary rows: %v", err)
+ }
+ if summaries != 1 {
+ t.Fatalf("summary rows = %d, want 1", summaries)
+ }
+}
+
+func TestFoldIngestionRunSkipsWhenProtectedRowsReachLimit(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertFoldPipelineLog(t, db, "run-1", entity.TaskStatusCancel, 1)
+ for _, component := range []string{"Parser", "Chunker", "Writer"} {
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeLifecycle, 0, component, "started")
+ }
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeTerminal, 0, "", "Task stopped by user.")
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, 0, "", "extra")
+
+ result, err := NewIngestionTaskService().foldIngestionRun(t.Context(), "run-1", 4)
+ if err != nil {
+ t.Fatalf("foldIngestionRun failed: %v", err)
+ }
+ if !result.Skipped || result.SkipReason != "protected_rows_reach_limit" {
+ t.Fatalf("result = %+v, want protected-row skip", result)
+ }
+ if got := countFoldEvents(t, db, "run-1"); got != 5 {
+ t.Fatalf("event count = %d, want 5", got)
+ }
+}
+
+func TestRecordTerminalFoldsTerminalRunAfterWritingEvent(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertFoldPipelineLog(t, db, "run-1", entity.TaskStatusDone, 1)
+ for i := 0; i < 8; i++ {
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, 0, "", "detail")
+ }
+
+ svc := NewIngestionTaskService()
+ if err := svc.SetIngestionLogSettings(IngestionLogSettings{
+ MaxRowsPerRun: 5,
+ MaxRowsPerDocument: 20,
+ MaxMessageChars: 4_000,
+ MaxMessageBytes: 16_384,
+ }); err != nil {
+ t.Fatalf("SetIngestionLogSettings failed: %v", err)
+ }
+ if err := svc.RecordTerminal(t.Context(), "run-1", "task-1", "Task completed."); err != nil {
+ t.Fatalf("RecordTerminal failed: %v", err)
+ }
+ if got := countFoldEvents(t, db, "run-1"); got != 5 {
+ t.Fatalf("event count = %d, want folded cap 5", got)
+ }
+ events := listFoldEvents(t, db, "run-1")
+ if !containsFoldMessage(events, "omitted 5 process messages") {
+ t.Fatalf("fold summary missing five omitted events: %+v", events)
+ }
+ var terminals int
+ for _, event := range events {
+ if event.EventType == dao.EventTypeTerminal && event.Message == "Task completed." {
+ terminals++
+ }
+ }
+ if terminals != 1 {
+ t.Fatalf("terminal events = %d, want 1", terminals)
+ }
+}
+
+func TestRecordTerminalDropsOldTerminalRunsOverDocumentCap(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertFoldPipelineLog(t, db, "run-1", entity.TaskStatusDone, 1)
+ insertFoldPipelineLog(t, db, "run-2", entity.TaskStatusDone, 2)
+ for i := 0; i < 5; i++ {
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, 0, "", "old detail")
+ insertFoldEvent(t, db, "run-2", "task-1", dao.EventTypeMessage, 0, "", "new detail")
+ }
+
+ svc := NewIngestionTaskService()
+ if err := svc.SetIngestionLogSettings(IngestionLogSettings{
+ MaxRowsPerRun: 5,
+ MaxRowsPerDocument: 5,
+ MaxMessageChars: 4_000,
+ MaxMessageBytes: 16_384,
+ }); err != nil {
+ t.Fatalf("SetIngestionLogSettings failed: %v", err)
+ }
+ if err := svc.RecordTerminal(t.Context(), "run-2", "task-1", "Task completed."); err != nil {
+ t.Fatalf("RecordTerminal failed: %v", err)
+ }
+ if got := countFoldEvents(t, db, "run-1"); got != 0 {
+ t.Fatalf("old run event count = %d, want entire run removed", got)
+ }
+ if got := countFoldEvents(t, db, "run-2"); got != 5 {
+ t.Fatalf("latest run event count = %d, want 5", got)
+ }
+ var oldRun entity.PipelineOperationLog
+ if err := db.First(&oldRun, "id = ?", "run-1").Error; err != nil {
+ t.Fatalf("old pipeline log was deleted: %v", err)
+ }
+}
+
+func TestTrimIngestionDocumentNeverDeletesRunningRun(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertFoldPipelineLog(t, db, "run-1", entity.TaskStatusDone, 1)
+ insertFoldPipelineLog(t, db, "run-2", entity.TaskStatusRunning, 2)
+ for i := 0; i < 4; i++ {
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, 0, "", "old detail")
+ insertFoldEvent(t, db, "run-2", "task-1", dao.EventTypeMessage, 0, "", "running detail")
+ }
+
+ result, err := NewIngestionTaskService().trimIngestionDocument(t.Context(), "doc-1", "run-2", 4)
+ if err != nil {
+ t.Fatalf("trimIngestionDocument failed: %v", err)
+ }
+ if result.DeletedRuns != 1 || result.DeletedEvents != 4 {
+ t.Fatalf("trim result = %+v, want one old run/four events deleted", result)
+ }
+ if got := countFoldEvents(t, db, "run-1"); got != 0 {
+ t.Fatalf("old terminal run events = %d, want 0", got)
+ }
+ if got := countFoldEvents(t, db, "run-2"); got != 4 {
+ t.Fatalf("running run event count = %d, want untouched", got)
+ }
+}
+
+func TestFoldIngestionRunScalesBudgetBeyondDefaultCap(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertFoldPipelineLog(t, db, "run-1", entity.TaskStatusDone, 1)
+
+ for i := 0; i < 12; i++ {
+ insertFoldEvent(t, db, "run-1", "task-1", dao.EventTypeMessage, 0, "", fmt.Sprintf("msg-%d", i))
+ }
+
+ result, err := NewIngestionTaskService().foldIngestionRun(t.Context(), "run-1", 10)
+ if err != nil {
+ t.Fatalf("foldIngestionRun failed: %v", err)
+ }
+ if result.Before != 12 || result.After != 10 || result.SummaryID == 0 {
+ t.Fatalf("result = %+v, want 12 -> 10 with summary", result)
+ }
+ if got := countFoldEvents(t, db, "run-1"); got != 10 {
+ t.Fatalf("event count = %d, want 10", got)
+ }
+ events := listFoldEvents(t, db, "run-1")
+ var hasHead, hasTailLast bool
+ for _, ev := range events {
+ if ev.Message == "msg-0" {
+ hasHead = true
+ }
+ if ev.Message == "msg-11" {
+ hasTailLast = true
+ }
+ }
+ if !hasHead || !hasTailLast {
+ t.Fatalf("events missing expected head or tail: %+v", events)
+ }
+}
+
+func insertFoldPipelineLog(t *testing.T, db *gorm.DB, id string, status entity.TaskStatus, runCount int) {
+ t.Helper()
+ if err := db.Create(&entity.PipelineOperationLog{
+ ID: id,
+ DocumentID: "doc-1",
+ TenantID: "tenant-1",
+ KbID: "kb-1",
+ ParserID: "naive",
+ DocumentName: "doc.txt",
+ DocumentSuffix: ".txt",
+ DocumentType: "text",
+ SourceFrom: "local",
+ TaskType: string(entity.PipelineTaskTypeParse),
+ OperationStatus: string(status),
+ RunCount: &runCount,
+ }).Error; err != nil {
+ t.Fatalf("insert pipeline log: %v", err)
+ }
+}
+
+func insertFoldEvent(t *testing.T, db *gorm.DB, runID, taskID string, eventType, phase int, component, message string) int {
+ t.Helper()
+ result := db.Model(&entity.IngestionTaskLog{}).Create(map[string]interface{}{
+ "task_id": taskID,
+ "pipeline_log_id": runID,
+ "checkpoint": entity.JSONMap{},
+ "event_type": eventType,
+ "component": component,
+ "phase": phase,
+ "message": message,
+ })
+ if result.Error != nil {
+ t.Fatalf("insert event: %v", result.Error)
+ }
+ var event entity.IngestionTaskLog
+ if err := db.Order("id DESC").First(&event).Error; err != nil {
+ t.Fatalf("load inserted event: %v", err)
+ }
+ return event.ID
+}
+
+func listFoldEvents(t *testing.T, db *gorm.DB, runID string) []*entity.IngestionTaskLog {
+ t.Helper()
+ events, err := dao.NewIngestionTaskLogDAO().ListLogsByPipelineLogID(t.Context(), db, runID)
+ if err != nil {
+ t.Fatalf("list events: %v", err)
+ }
+ return events
+}
+
+func countFoldEvents(t *testing.T, db *gorm.DB, runID string) int {
+ t.Helper()
+ var count int64
+ if err := db.Model(&entity.IngestionTaskLog{}).Where("pipeline_log_id = ?", runID).Count(&count).Error; err != nil {
+ t.Fatalf("count events: %v", err)
+ }
+ return int(count)
+}
diff --git a/internal/service/ingestion_log_settings.go b/internal/service/ingestion_log_settings.go
new file mode 100644
index 0000000000..8433d21306
--- /dev/null
+++ b/internal/service/ingestion_log_settings.go
@@ -0,0 +1,49 @@
+package service
+
+import "fmt"
+
+// IngestionLogSettings controls event message bounds and terminal-run
+// retention. The defaults match the rollout plan and are also used by tests
+// and callers that construct an IngestionTaskService directly.
+type IngestionLogSettings struct {
+ MaxRowsPerRun int
+ MaxRowsPerDocument int
+ MaxMessageChars int
+ MaxMessageBytes int
+}
+
+func DefaultIngestionLogSettings() IngestionLogSettings {
+ return IngestionLogSettings{
+ MaxRowsPerRun: 5_000,
+ MaxRowsPerDocument: 20_000,
+ MaxMessageChars: 4_000,
+ MaxMessageBytes: 16_384,
+ }
+}
+
+func (s IngestionLogSettings) validate() error {
+ if s.MaxRowsPerRun < 2 {
+ return fmt.Errorf("ingestion log max rows per run must be at least 2")
+ }
+ if s.MaxRowsPerDocument < s.MaxRowsPerRun {
+ return fmt.Errorf("ingestion log max rows per document must be >= max rows per run")
+ }
+ if s.MaxMessageChars <= 0 {
+ return fmt.Errorf("ingestion log max message chars must be positive")
+ }
+ if s.MaxMessageBytes <= 0 {
+ return fmt.Errorf("ingestion log max message bytes must be positive")
+ }
+ return nil
+}
+
+// SetIngestionLogSettings validates and replaces the service's event and
+// retention limits. It is intended to be called during startup before the
+// service is used by workers.
+func (s *IngestionTaskService) SetIngestionLogSettings(settings IngestionLogSettings) error {
+ if err := settings.validate(); err != nil {
+ return err
+ }
+ s.logSettings = settings
+ return nil
+}
diff --git a/internal/service/ingestion_task_service.go b/internal/service/ingestion_task_service.go
index 429950db7d..44374fc89b 100644
--- a/internal/service/ingestion_task_service.go
+++ b/internal/service/ingestion_task_service.go
@@ -12,13 +12,22 @@ import (
redis2 "ragflow/internal/engine/redis"
"ragflow/internal/entity"
+ "go.uber.org/zap"
"gorm.io/gorm"
)
-// Run-count key for IngestionTaskLog.Checkpoint, consumed by
-// ListAllForAdmin and IncrementRunCount to track how many times
-// the task has been picked up by a worker.
-const stepKeyRunCount = "run_count"
+// ingestionEventKind intentionally has an invalid zero value. The persisted
+// event protocol starts at zero for lifecycle events, so callers cannot pass a
+// Go zero value and silently create a lifecycle row.
+type ingestionEventKind uint8
+
+const (
+ ingestionEventInvalid ingestionEventKind = iota
+ ingestionEventLifecycle
+ ingestionEventMessage
+ ingestionEventTerminal
+ ingestionEventSystem
+)
type InvalidTaskTransitionError struct {
TaskID string
@@ -37,6 +46,18 @@ type TaskStatusConflictError struct {
ActualCurrent string
}
+// InvalidRunIdentityError reports a durable invariant violation discovered
+// after a worker moves a task to RUNNING. Callers distinguish it from storage
+// errors: the former must be settled as FAILED, while the latter is retried.
+type InvalidRunIdentityError struct {
+ TaskID string
+ Reason string
+}
+
+func (e *InvalidRunIdentityError) Error() string {
+ return fmt.Sprintf("task %s has invalid run identity: %s", e.TaskID, e.Reason)
+}
+
func (e *TaskStatusConflictError) Error() string {
return fmt.Sprintf("task %s status conflict: expected %s -> %s, actual current %s", e.TaskID, e.ExpectedFrom, e.AttemptedTo, e.ActualCurrent)
}
@@ -50,6 +71,7 @@ type IngestionTaskService struct {
kbDAO *dao.KnowledgebaseDAO
userCanvasDAO *dao.UserCanvasDAO
taskPublisher TaskPublisher
+ logSettings IngestionLogSettings
}
func NewIngestionTaskService() *IngestionTaskService {
@@ -62,6 +84,7 @@ func NewIngestionTaskService() *IngestionTaskService {
kbDAO: dao.NewKnowledgebaseDAO(),
userCanvasDAO: dao.NewUserCanvasDAO(),
taskPublisher: NewMessageQueueTaskPublisher(),
+ logSettings: DefaultIngestionLogSettings(),
}
}
@@ -85,8 +108,7 @@ func (s *IngestionTaskService) CreateForDocuments(ctx context.Context, datasetID
return nil, fmt.Errorf("no documents to parse")
}
- // Open-log metadata is best-effort. Populate this cache lazily so a missing
- // or temporarily unavailable knowledge base cannot block task creation.
+ // Populate this cache lazily so a batch avoids repeated knowledge-base reads.
kbCache := make(map[string]*entity.Knowledgebase)
responses := make([]*ParseDocumentResponse, 0, len(uniqueDocIDs))
@@ -188,13 +210,20 @@ func (s *IngestionTaskService) ListAllForAdmin(ctx context.Context) ([]map[strin
"status": task.Status,
}
- if count, ok := s.lastRunCount(ctx, task.ID); ok {
- showTask["run_count"] = count
+ if task.PipelineLogID != nil && *task.PipelineLogID != "" {
+ if run, runErr := s.pipelineLogDAO.GetByID(ctx, dao.DB, *task.PipelineLogID); runErr == nil && run.RunCount != nil && *run.RunCount > 0 {
+ showTask["run_count"] = *run.RunCount
+ }
}
showTask["component_total"] = task.ComponentTotal
if task.ComponentTotal > 0 {
- progress, err := s.ingestionTaskLogDAO.AggregateProgress(ctx, dao.DB, task.ID, task.ComponentTotal)
+ progress := (*dao.TaskProgress)(nil)
+ if task.PipelineLogID != nil && *task.PipelineLogID != "" {
+ progress, err = s.ingestionTaskLogDAO.AggregateProgressByPipelineLogID(ctx, dao.DB, *task.PipelineLogID, task.ComponentTotal)
+ } else {
+ err = errors.New("task has no pipeline log identity")
+ }
if err == nil {
showTask["component_done"] = progress.Done
} else {
@@ -209,7 +238,10 @@ func (s *IngestionTaskService) ListAllForAdmin(ctx context.Context) ([]map[strin
return showTasks, nil
}
-func (s *IngestionTaskService) StartRunning(ctx context.Context, taskID string) (*entity.IngestionTask, error) {
+// TransitionTaskToRunning performs only the task-state transition required
+// before worker identity validation. It intentionally does not reset the
+// document or advance a pipeline log.
+func (s *IngestionTaskService) TransitionTaskToRunning(ctx context.Context, taskID string) (*entity.IngestionTask, error) {
task, err := s.GetTask(ctx, taskID)
if err != nil {
return nil, err
@@ -220,19 +252,6 @@ func (s *IngestionTaskService) StartRunning(ctx context.Context, taskID string)
if err != nil {
return nil, err
}
- // The task just started running: reset document progress counters to
- // reflect real processing. Best-effort - a DB blip here must not fail
- // the task transition and trigger a redelivery loop.
- if err = s.documentDAO.UpdateByID(ctx, dao.DB, task.DocumentID, map[string]interface{}{
- "progress": float64(0),
- "chunk_num": int64(0),
- "token_num": int64(0),
- "process_begin_at": time.Now(),
- "progress_msg": "",
- }); err != nil {
- common.Warn(fmt.Sprintf("StartRunning: mark document %s running for task %s: %v", task.DocumentID, taskID, err))
- }
- s.advanceOpenLog(ctx, task, logFromUnstartOrScheduled, string(entity.TaskStatusRunning), logMsgRunning, true)
return task, nil
case common.STOPPING:
task, err = s.transition(ctx, taskID, common.STOPPED)
@@ -247,7 +266,8 @@ func (s *IngestionTaskService) StartRunning(ctx context.Context, taskID string)
// Same reason as RequestStop's CREATED/SCHEDULED branch: the stop
// finalizes without a worker, so no terminal pipeline-log writer will
// close the open row. Close it here or it stays RUNNING forever.
- s.advanceOpenLog(ctx, task, dao.OpenPipelineOperationStatuses(), string(entity.TaskStatusCancel), logMsgStopped, false)
+ s.advanceOpenLog(ctx, task, dao.OpenPipelineOperationStatuses(), string(entity.TaskStatusCancel))
+ s.recordRunTerminal(ctx, task, "Task stopped by user.")
return task, nil
case common.RUNNING, common.COMPLETED, common.STOPPED, common.FAILED:
return task, nil
@@ -256,6 +276,24 @@ func (s *IngestionTaskService) StartRunning(ctx context.Context, taskID string)
}
}
+// PrepareValidatedRun performs the document and run-log initialization only
+// after ReloadAndValidateRunIdentity has accepted the task's captured binding.
+func (s *IngestionTaskService) PrepareValidatedRun(ctx context.Context, task *entity.IngestionTask) {
+ if task == nil {
+ return
+ }
+ if err := s.documentDAO.UpdateByID(ctx, dao.DB, task.DocumentID, map[string]interface{}{
+ "progress": float64(0),
+ "chunk_num": int64(0),
+ "token_num": int64(0),
+ "process_begin_at": time.Now(),
+ }); err != nil {
+ common.Warn(fmt.Sprintf("prepare validated run: mark document %s running for task %s: %v", task.DocumentID, task.ID, err))
+ }
+ s.advanceOpenLog(ctx, task, logFromUnstartOrScheduled, string(entity.TaskStatusRunning))
+ s.recordRunMessage(ctx, task, "Task is running...")
+}
+
func (s *IngestionTaskService) RequestStop(ctx context.Context, taskID string) (*entity.IngestionTask, error) {
task, err := s.GetTask(ctx, taskID)
if err != nil {
@@ -270,7 +308,8 @@ func (s *IngestionTaskService) RequestStop(ctx context.Context, taskID string) (
// The stop finalizes without a worker (no RUNNING phase, so no
// terminal pipeline-log writer will run). Advance the open row to
// CANCEL here, otherwise the detail page keeps a queued entry.
- s.advanceOpenLog(ctx, stopped, logFromUnstartOrScheduled, string(entity.TaskStatusCancel), logMsgStopped, false)
+ s.advanceOpenLog(ctx, stopped, logFromUnstartOrScheduled, string(entity.TaskStatusCancel))
+ s.recordRunTerminal(ctx, stopped, "Task stopped by user.")
return stopped, nil
case common.RUNNING:
task, err = s.transition(ctx, taskID, common.STOPPING)
@@ -289,6 +328,30 @@ func (s *IngestionTaskService) RequestStop(ctx context.Context, taskID string) (
}
}
+// SupersedeUnstartedTask closes a queued run before a replacement parse is
+// created. A numbered pipeline log is an immutable run ledger, so this must
+// record its cancellation rather than deleting the row with the task.
+func (s *IngestionTaskService) SupersedeUnstartedTask(ctx context.Context, taskID string) error {
+ task, err := s.GetTask(ctx, taskID)
+ if err != nil {
+ return err
+ }
+ switch task.Status {
+ case common.CREATED, common.SCHEDULED:
+ stopped, err := s.transition(ctx, taskID, common.STOPPED)
+ if err != nil {
+ return err
+ }
+ s.advanceOpenLog(ctx, stopped, dao.OpenPipelineOperationStatuses(), string(entity.TaskStatusCancel))
+ s.recordRunTerminal(ctx, stopped, "Task superseded by a new parse request.")
+ return nil
+ case common.RUNNING, common.STOPPING:
+ return fmt.Errorf("task %s is %s and cannot be superseded", taskID, task.Status)
+ default:
+ return nil
+ }
+}
+
func (s *IngestionTaskService) MarkCompleted(ctx context.Context, taskID string) error {
task, err := s.GetTask(ctx, taskID)
if err != nil {
@@ -333,14 +396,29 @@ func (s *IngestionTaskService) Remove(ctx context.Context, taskID string, userID
if err != nil {
return nil, err
}
+ if userID != nil && task.UserID != *userID {
+ return nil, errors.New("task does not belong to the user")
+ }
+ // Numbered pipeline logs are immutable run ledgers. A queued run must be
+ // settled before its task row is deleted, otherwise no worker remains to
+ // write the cancellation terminal event and the log stays open forever.
+ if task.Status == common.CREATED || task.Status == common.SCHEDULED {
+ if task.PipelineLogID != nil && *task.PipelineLogID != "" {
+ run, runErr := s.pipelineLogDAO.GetByID(ctx, dao.DB, *task.PipelineLogID)
+ if runErr != nil {
+ return nil, runErr
+ }
+ if run.RunCount != nil && *run.RunCount > 0 {
+ if err := s.SupersedeUnstartedTask(ctx, taskID); err != nil {
+ return nil, err
+ }
+ }
+ }
+ }
info, err := s.ingestionTaskDAO.Delete(ctx, dao.DB, taskID, userID)
if err != nil {
return nil, err
}
- // The task row is gone, so no worker will ever reach the terminal writer
- // for this run. Drop its open row, otherwise the detail page keeps a
- // queued entry.
- s.deleteOpenLogBestEffort(ctx, task)
return info, nil
}
@@ -355,14 +433,45 @@ func (s *IngestionTaskService) GetTask(ctx context.Context, taskID string) (*ent
return task, nil
}
+// ReloadAndValidateRunIdentity reloads a worker task and verifies that its
+// immutable run binding exists, belongs to the same document/dataset, and has
+// a positive Go-owned display number. It performs no writes and never creates
+// a replacement row; callers can safely retry transient database errors.
+func (s *IngestionTaskService) ReloadAndValidateRunIdentity(ctx context.Context, taskID string) (*entity.IngestionTask, error) {
+ task, err := s.GetTask(ctx, taskID)
+ if err != nil {
+ return nil, err
+ }
+ if task.PipelineLogID == nil || *task.PipelineLogID == "" {
+ return nil, &InvalidRunIdentityError{TaskID: taskID, Reason: "missing_pipeline_log_id"}
+ }
+ run, err := s.pipelineLogDAO.GetByID(ctx, dao.DB, *task.PipelineLogID)
+ if err != nil {
+ if dao.IsNotFoundErr(err) {
+ return nil, &InvalidRunIdentityError{TaskID: taskID, Reason: "pipeline_log_not_found"}
+ }
+ return nil, err
+ }
+ if run.DocumentID != task.DocumentID {
+ return nil, &InvalidRunIdentityError{TaskID: taskID, Reason: "pipeline_log_document_mismatch"}
+ }
+ if run.KbID != task.DatasetID {
+ return nil, &InvalidRunIdentityError{TaskID: taskID, Reason: "pipeline_log_dataset_mismatch"}
+ }
+ if run.RunCount == nil || *run.RunCount <= 0 {
+ return nil, &InvalidRunIdentityError{TaskID: taskID, Reason: "invalid_run_count"}
+ }
+ return task, nil
+}
+
func validateTransition(from, to string) error {
switch from {
case common.CREATED:
- if to == common.SCHEDULED || to == common.RUNNING || to == common.STOPPED {
+ if to == common.SCHEDULED || to == common.RUNNING || to == common.STOPPED || to == common.FAILED {
return nil
}
case common.SCHEDULED:
- if to == common.RUNNING || to == common.STOPPED {
+ if to == common.RUNNING || to == common.STOPPED || to == common.FAILED {
return nil
}
case common.RUNNING:
@@ -453,12 +562,11 @@ func (s *IngestionTaskService) createAndEnqueueWithKBCache(ctx context.Context,
if existing != nil {
switch existing.Status {
case common.CREATED:
- // An existing CREATED task may already carry a queued row from
- // before a crash; the helper adopts it (binding the task to it)
- // instead of opening a second row, then the schedule transition
- // below advances it.
- s.createOpenLogBestEffort(ctx, existing, string(entity.TaskStatusUnstart), logMsgQueued, kbCache)
+ if err = s.ensureRunIdentity(ctx, existing, kbCache); err != nil {
+ return nil, fmt.Errorf("ensure run identity for task %s: %w", existing.ID, err)
+ }
if err = s.enqueueTask(existing.ID); err != nil {
+ s.settlePublishFailure(ctx, existing)
return nil, err
}
return s.markScheduledAfterPublish(ctx, existing.ID)
@@ -468,20 +576,23 @@ func (s *IngestionTaskService) createAndEnqueueWithKBCache(ctx context.Context,
if err != nil {
return nil, err
}
+ if err = s.ingestionTaskDAO.ClearPipelineLogID(ctx, dao.DB, existing.ID); err != nil {
+ return nil, fmt.Errorf("clear previous run identity for task %s: %w", existing.ID, err)
+ }
+ existing.PipelineLogID = nil
// The previous run is terminal, so any leftover Redis cancel flag
// is stale: a genuine cancel of the new run can only come through
// RequestStop once the task is RUNNING again. Clear it so the
// re-queued task is not cancelled at the worker's pre-start check.
clearCancelFlag(ctx, existing.ID)
- // A retry starts a fresh pipeline-operation-log row: the prior
- // run's row is terminal, so no open row matches and the helper
- // opens (and binds) a new one.
- s.createOpenLogBestEffort(ctx, existing, string(entity.TaskStatusUnstart), logMsgQueued, kbCache)
- if err = s.enqueueTask(existing.ID); err != nil {
- s.deleteOpenLogBestEffort(ctx, existing)
+ if err = s.ensureRunIdentity(ctx, existing, kbCache); err != nil {
if rollbackErr := s.rollbackRetriedTask(ctx, existing.ID, originalStatus); rollbackErr != nil {
- return nil, fmt.Errorf("enqueue task %s: %w (rollback failed: %w)", existing.ID, err, rollbackErr)
+ return nil, fmt.Errorf("ensure run identity for task %s: %w (rollback failed: %v)", existing.ID, err, rollbackErr)
}
+ return nil, fmt.Errorf("ensure run identity for task %s: %w", existing.ID, err)
+ }
+ if err = s.enqueueTask(existing.ID); err != nil {
+ s.settlePublishFailure(ctx, existing)
return nil, err
}
return s.markScheduledAfterPublish(ctx, existing.ID)
@@ -494,19 +605,14 @@ func (s *IngestionTaskService) createAndEnqueueWithKBCache(ctx context.Context,
if err != nil {
return nil, err
}
- // Open the pre-terminal row the dataset detail page reads *before*
- // publishing. Once the message is out a worker can claim the task and run
- // it to a terminal state, and the terminal writer must always find the row
- // it is bound to; a row created after publication could be inserted too
- // late and stay queued forever. The prior run (if any) is terminal, so no
- // open row exists and this never duplicates. Best-effort: on a DB blip the
- // row is simply missing and markScheduledAfterPublish re-opens it at "5".
- s.createOpenLogBestEffort(ctx, created, string(entity.TaskStatusUnstart), logMsgQueued, kbCache)
- if err = s.enqueueTask(created.ID); err != nil {
- s.deleteOpenLogBestEffort(ctx, created)
+ if err = s.ensureRunIdentity(ctx, created, kbCache); err != nil {
if rollbackErr := s.rollbackCreatedTask(ctx, created.ID); rollbackErr != nil {
- return nil, fmt.Errorf("enqueue task %s: %w (rollback failed: %w)", created.ID, err, rollbackErr)
+ return nil, fmt.Errorf("ensure run identity for task %s: %w (rollback failed: %v)", created.ID, err, rollbackErr)
}
+ return nil, fmt.Errorf("ensure run identity for task %s: %w", created.ID, err)
+ }
+ if err = s.enqueueTask(created.ID); err != nil {
+ s.settlePublishFailure(ctx, created)
return nil, err
}
return s.markScheduledAfterPublish(ctx, created.ID)
@@ -542,7 +648,8 @@ func (s *IngestionTaskService) markScheduledAfterPublish(ctx context.Context, ta
if err != nil {
return nil, err
}
- s.advanceOpenLog(ctx, task, logFromUnstart, string(entity.TaskStatusSchedule), logMsgQueued, true)
+ s.advanceOpenLog(ctx, task, logFromUnstart, string(entity.TaskStatusSchedule))
+ s.recordRunMessage(ctx, task, "Task is queued...")
return task, nil
}
@@ -573,7 +680,12 @@ func (s *IngestionTaskService) ScheduleCreatedTasks(ctx context.Context) error {
}
var recoveryErr error
for _, task := range tasks {
+ if err := s.ensureRunIdentity(ctx, task, nil); err != nil {
+ recoveryErr = errors.Join(recoveryErr, fmt.Errorf("ensure run identity for created task %s: %w", task.ID, err))
+ continue
+ }
if err := s.enqueueTask(task.ID); err != nil {
+ s.settlePublishFailure(ctx, task)
recoveryErr = errors.Join(recoveryErr, fmt.Errorf("schedule created task %s: %w", task.ID, err))
continue
}
@@ -584,6 +696,22 @@ func (s *IngestionTaskService) ScheduleCreatedTasks(ctx context.Context) error {
return recoveryErr
}
+// settlePublishFailure closes the already-numbered run after a broker publish
+// failure. The number is an immutable document ledger entry, so rolling the
+// task back or deleting the log would allow a future request to reuse it.
+func (s *IngestionTaskService) settlePublishFailure(ctx context.Context, task *entity.IngestionTask) {
+ if task == nil {
+ return
+ }
+ if err := s.MarkFailed(ctx, task.ID); err != nil {
+ common.Error(fmt.Sprintf("mark task %s failed after publish failure", task.ID), err)
+ return
+ }
+ task.Status = common.FAILED
+ s.advanceOpenLog(ctx, task, dao.OpenPipelineOperationStatuses(), string(entity.TaskStatusFail))
+ s.recordRunTerminal(ctx, task, "Task publish failed.")
+}
+
// clearCancelFlag removes the Redis cancel marker ({task_id}-cancel) that
// RequestStop sets for a RUNNING task. No-op when Redis is unavailable —
// the DB STOPPING status remains the fallback cancel signal.
@@ -601,13 +729,6 @@ func (s *IngestionTaskService) enqueueTask(taskID string) error {
return s.taskPublisher.PublishTaskMessage(common.TaskSubject, taskMessage)
}
-// Messages the queued stages show on the dataset detail page.
-const (
- logMsgQueued = "Task is queued..."
- logMsgRunning = "Task is running..."
- logMsgStopped = "Task stopped by user."
-)
-
// logFromUnstart and logFromUnstartOrScheduled are the operation_status values
// an open pipeline-log row may carry when a stage advances it. Scoping each
// advance to its valid from-states keeps the transitions monotonic
@@ -619,59 +740,52 @@ var (
logFromUnstartOrScheduled = []string{string(entity.TaskStatusUnstart), string(entity.TaskStatusSchedule)}
)
-// createOpenLogBestEffort opens the pre-terminal row the dataset detail page
-// reads and binds it to the task. Best-effort: never fails task creation. The
-// caller passes the status/message so a late-created row carries the current
-// status.
-//
-// Ownership establishment is serialized on the task row. A stale in-memory
-// task snapshot therefore cannot replace a binding another caller just wrote.
-// An unbound run never adopts a document-level row; it removes that row only
-// when no live task owns it, then creates and binds its own row atomically.
-func (s *IngestionTaskService) createOpenLogBestEffort(ctx context.Context, task *entity.IngestionTask, operationStatus, progressMsg string, kbCache map[string]*entity.Knowledgebase) {
- if task == nil || s.pipelineLogDAO == nil {
- return
- }
- input, err := s.buildOpenLogInput(ctx, task, operationStatus, progressMsg, kbCache)
+// ensureRunIdentity is the only creation path for a Go ingestion run. It is
+// called before publication, never by a worker. A document row lock serializes
+// MAX(run_count)+1 allocation, then the task is bound to the created row in
+// the same short transaction.
+func (s *IngestionTaskService) ensureRunIdentity(ctx context.Context, task *entity.IngestionTask, kbCache map[string]*entity.Knowledgebase) error {
+ if task == nil || task.ID == "" || task.DocumentID == "" || s.pipelineLogDAO == nil {
+ return errors.New("run identity requires a task, document, and pipeline log DAO")
+ }
+ input, err := s.buildOpenLogInput(ctx, task, kbCache)
if err != nil {
- common.Warn(fmt.Sprintf("open pipeline log: build input for task %s document %s: %v", task.ID, task.DocumentID, err))
- return
+ return err
}
+ input.OperationStatus = string(entity.TaskStatusUnstart)
var boundLogID string
err = dao.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ lockedDocument, err := s.documentDAO.GetByIDForUpdate(ctx, tx, task.DocumentID)
+ if err != nil {
+ return err
+ }
lockedTask, err := s.ingestionTaskDAO.GetByIDForUpdate(ctx, tx, task.ID)
if err != nil {
return err
}
+ if lockedTask.DocumentID != lockedDocument.ID || lockedTask.DatasetID != input.KbID {
+ return fmt.Errorf("task %s no longer matches document or dataset", lockedTask.ID)
+ }
if isTerminalIngestionTask(lockedTask.Status) {
- return nil
+ return fmt.Errorf("task %s is terminal", lockedTask.ID)
}
if lockedTask.PipelineLogID != nil && *lockedTask.PipelineLogID != "" {
bound, err := s.pipelineLogDAO.GetByID(ctx, tx, *lockedTask.PipelineLogID)
- if err == nil && isOpenPipelineLog(bound.OperationStatus) {
- boundLogID = bound.ID
- return nil
- }
- if err != nil && !dao.IsNotFoundErr(err) {
+ if err != nil {
return err
}
+ if bound.DocumentID != lockedTask.DocumentID || bound.KbID != input.KbID || bound.RunCount == nil || *bound.RunCount <= 0 {
+ return fmt.Errorf("task %s has invalid pipeline run binding %s", lockedTask.ID, bound.ID)
+ }
+ boundLogID = bound.ID
+ return nil
}
- open, err := s.pipelineLogDAO.GetOpenLogByDocumentID(ctx, tx, lockedTask.DocumentID)
+ runCount, err := s.pipelineLogDAO.NextRunCount(ctx, tx, lockedTask.DocumentID)
if err != nil {
return err
}
- if open != nil {
- deleted, err := s.pipelineLogDAO.DeleteUnownedOpenLogByID(ctx, tx, open.ID)
- if err != nil {
- return err
- }
- if !deleted {
- return fmt.Errorf("open pipeline log %s is owned by another live task", open.ID)
- }
- common.Warn(fmt.Sprintf("dropped unowned open pipeline log %s for document %s (status %s)", open.ID, lockedTask.DocumentID, open.OperationStatus))
- }
-
+ input.RunCount = runCount
log, err := s.pipelineLogDAO.CreateOpenLog(ctx, tx, input)
if err != nil {
return err
@@ -683,33 +797,23 @@ func (s *IngestionTaskService) createOpenLogBestEffort(ctx context.Context, task
return nil
})
if err != nil {
- common.Warn(fmt.Sprintf("open pipeline log: establish ownership for task %s document %s: %v", task.ID, task.DocumentID, err))
- return
+ return err
}
- if boundLogID != "" {
- task.PipelineLogID = &boundLogID
+ if boundLogID == "" {
+ return errors.New("run identity was not bound")
}
+ task.PipelineLogID = &boundLogID
+ return nil
}
func isTerminalIngestionTask(status string) bool {
return status == common.COMPLETED || status == common.STOPPED || status == common.FAILED
}
-func isOpenPipelineLog(status string) bool {
- return status == string(entity.TaskStatusUnstart) ||
- status == string(entity.TaskStatusSchedule) ||
- status == string(entity.TaskStatusRunning)
-}
-
-// advanceOpenLog moves the task's own bound row to operationStatus from one of
-// fromStatuses. Best-effort: never fails task transitions.
-//
-// It never looks the row up by document: only the row this run is bound to is
-// advanced, so a concurrent run's row can be neither regressed nor adopted. A
-// task with no bound row (legacy task, or a lost best-effort create) opens one,
-// but only while the task is still live — a terminal task must never resurrect
-// a closed row as a permanently queued entry.
-func (s *IngestionTaskService) advanceOpenLog(ctx context.Context, task *entity.IngestionTask, fromStatuses []string, operationStatus, progressMsg string, reopen bool) {
+// advanceOpenLog moves a task's already-bound row to operationStatus. It never
+// creates or adopts a row, so a worker cannot repair a missing identity or
+// affect a newer run.
+func (s *IngestionTaskService) advanceOpenLog(ctx context.Context, task *entity.IngestionTask, fromStatuses []string, operationStatus string) {
if task == nil || task.DocumentID == "" || s.pipelineLogDAO == nil {
return
}
@@ -718,35 +822,36 @@ func (s *IngestionTaskService) advanceOpenLog(ctx context.Context, task *entity.
logID = *task.PipelineLogID
}
if logID == "" {
- terminal := task.Status == common.COMPLETED || task.Status == common.STOPPED || task.Status == common.FAILED
- if !reopen || terminal {
- return
- }
- s.createOpenLogBestEffort(ctx, task, operationStatus, progressMsg, nil)
+ common.Warn(fmt.Sprintf("advance open pipeline log for task %s: missing run identity", task.ID))
return
}
- if err := s.pipelineLogDAO.AdvanceOpenLog(ctx, dao.DB, logID, fromStatuses, operationStatus, progressMsg); err != nil {
+ if err := s.pipelineLogDAO.AdvanceOpenLog(ctx, dao.DB, logID, fromStatuses, operationStatus); err != nil {
common.Warn(fmt.Sprintf("advance open pipeline log for document %s to %s: %v", task.DocumentID, operationStatus, err))
}
}
-// deleteOpenLogBestEffort drops the open row a run owns, so a rolled-back or
-// deleted run leaves no permanently queued entry on the detail page. Only the
-// run's own open row goes: a terminal row is history, and a newer run's row is
-// not this caller's to drop.
-func (s *IngestionTaskService) deleteOpenLogBestEffort(ctx context.Context, task *entity.IngestionTask) {
- if task == nil || task.PipelineLogID == nil || s.pipelineLogDAO == nil {
+func (s *IngestionTaskService) recordRunMessage(ctx context.Context, task *entity.IngestionTask, message string) {
+ if task == nil || task.PipelineLogID == nil || *task.PipelineLogID == "" {
+ return
+ }
+ if err := s.RecordMessage(ctx, *task.PipelineLogID, task.ID, message); err != nil {
+ common.Warn(fmt.Sprintf("record run message for task %s: %v", task.ID, err))
+ }
+}
+
+func (s *IngestionTaskService) recordRunTerminal(ctx context.Context, task *entity.IngestionTask, message string) {
+ if task == nil || task.PipelineLogID == nil || *task.PipelineLogID == "" {
return
}
- if err := s.pipelineLogDAO.DeleteOpenLogByID(ctx, dao.DB, *task.PipelineLogID); err != nil {
- common.Warn(fmt.Sprintf("CreateAndEnqueue: delete open pipeline log for document %s: %v", task.DocumentID, err))
+ if err := s.RecordTerminal(ctx, *task.PipelineLogID, task.ID, message); err != nil {
+ common.Warn(fmt.Sprintf("record run terminal event for task %s: %v", task.ID, err))
}
}
-// buildOpenLogInput resolves the pre-terminal row from the document and its
-// knowledge base. Status/message come from the caller so a late-created row
-// carries the current status. DSL and progress stay empty for the terminal write.
-func (s *IngestionTaskService) buildOpenLogInput(ctx context.Context, task *entity.IngestionTask, operationStatus, progressMsg string, kbCache map[string]*entity.Knowledgebase) (dao.OpenLogInput, error) {
+// buildOpenLogInput resolves immutable run metadata from the document and its
+// knowledge base. The queued status and allocated run number are assigned by
+// EnsureRunIdentity in its short database transaction.
+func (s *IngestionTaskService) buildOpenLogInput(ctx context.Context, task *entity.IngestionTask, kbCache map[string]*entity.Knowledgebase) (dao.OpenLogInput, error) {
var input dao.OpenLogInput
doc, err := s.documentDAO.GetByID(ctx, dao.DB, task.DocumentID)
if err != nil {
@@ -770,16 +875,14 @@ func (s *IngestionTaskService) buildOpenLogInput(ctx context.Context, task *enti
}
}
input = dao.OpenLogInput{
- DocumentID: doc.ID,
- KbID: kbID,
- TenantID: kb.TenantID,
- ParserID: doc.ParserID,
- DocumentSuffix: doc.Suffix,
- DocumentType: doc.Type,
- OperationStatus: operationStatus,
- ProgressMsg: progressMsg,
- PipelineTitle: doc.ParserID,
- Avatar: doc.Thumbnail,
+ DocumentID: doc.ID,
+ KbID: kbID,
+ TenantID: kb.TenantID,
+ ParserID: doc.ParserID,
+ DocumentSuffix: doc.Suffix,
+ DocumentType: doc.Type,
+ PipelineTitle: doc.ParserID,
+ Avatar: doc.Thumbnail,
}
if doc.Name != nil {
input.DocumentName = *doc.Name
@@ -809,65 +912,144 @@ func (s *IngestionTaskService) UpdateComponentTotal(ctx context.Context, taskID
return s.ingestionTaskDAO.UpdateComponentTotal(ctx, dao.DB, taskID, total)
}
-// RecordComponentProgress appends a component lifecycle row to
-// ingestion_task_log (phase: 0 started / 1 done / 2 errored). The row's
-// Checkpoint is empty; component progress and step checkpoints are distinct
-// row models sharing the same table.
-func (s *IngestionTaskService) RecordComponentProgress(ctx context.Context, taskID, component string, phase int, message string) error {
- entry := &entity.IngestionTaskLog{
- TaskID: taskID,
- Checkpoint: entity.JSONMap{},
- Phase: phase,
- Component: component,
- Message: message,
+// RecordLifecycle writes one component lifecycle event for an already
+// validated run. The worker supplies the captured pipeline log ID, never a
+// value reloaded from the mutable task row.
+func (s *IngestionTaskService) RecordLifecycle(ctx context.Context, pipelineLogID, taskID, component string, phase int, message string) error {
+ if component == "" {
+ return rejectIngestionEvent("missing_component", pipelineLogID, taskID, errors.New("ingestion lifecycle event requires a component"))
+ }
+ if phase < 0 || phase > 2 {
+ return rejectIngestionEvent("invalid_phase", pipelineLogID, taskID, fmt.Errorf("ingestion lifecycle event has invalid phase %d", phase))
}
- return s.ingestionTaskLogDAO.Create(ctx, dao.DB, entry)
+ return s.insertEvent(ctx, ingestionEventLifecycle, pipelineLogID, taskID, component, phase, message)
+}
+
+// RecordMessage writes supplemental process detail without affecting component
+// progress aggregation.
+func (s *IngestionTaskService) RecordMessage(ctx context.Context, pipelineLogID, taskID, message string) error {
+ return s.insertEvent(ctx, ingestionEventMessage, pipelineLogID, taskID, "", 0, message)
}
-// ClearComponentProgress removes lifecycle rows left by a previous attempt of
-// the same reusable ingestion task. Run-count checkpoint rows are retained.
-func (s *IngestionTaskService) ClearComponentProgress(ctx context.Context, taskID string) error {
- _, err := s.ingestionTaskLogDAO.DeleteComponentLogsByTaskID(ctx, dao.DB, taskID)
+// RecordTerminal writes the terminal explanation associated with a run.
+func (s *IngestionTaskService) RecordTerminal(ctx context.Context, pipelineLogID, taskID, message string) error {
+ if err := s.insertEvent(ctx, ingestionEventTerminal, pipelineLogID, taskID, "", 0, message); err != nil {
+ return err
+ }
+ foldContext := context.WithoutCancel(ctx)
+ foldStarted := time.Now()
+ foldResult, foldErr := s.foldIngestionRun(foldContext, pipelineLogID, s.logSettings.MaxRowsPerRun)
+ logIngestionFoldResult(taskID, pipelineLogID, foldResult, time.Since(foldStarted), foldErr)
+ if foldErr != nil {
+ common.Warn(fmt.Sprintf("fold terminal ingestion run %s: %v", pipelineLogID, foldErr))
+ }
+ run, err := s.pipelineLogDAO.GetByID(foldContext, dao.DB, pipelineLogID)
+ if err != nil {
+ common.Warn(fmt.Sprintf("load terminal ingestion run %s for document trimming: %v", pipelineLogID, err))
+ logIngestionTrimResult("", taskID, pipelineLogID, ingestionDocumentTrimResult{}, 0, err)
+ return nil
+ }
+ trimStarted := time.Now()
+ trimResult, trimErr := s.trimIngestionDocument(foldContext, run.DocumentID, pipelineLogID, s.logSettings.MaxRowsPerDocument)
+ logIngestionTrimResult(run.DocumentID, taskID, pipelineLogID, trimResult, time.Since(trimStarted), trimErr)
+ if trimErr != nil {
+ common.Warn(fmt.Sprintf("trim ingestion document %s after terminal run %s: %v", run.DocumentID, pipelineLogID, trimErr))
+ }
+ return nil
+}
+
+func rejectIngestionEvent(reason, pipelineLogID, taskID string, err error) error {
+ common.Warn("ingestion_log_event_rejected",
+ zap.String("event", "ingestion_log_event_rejected"),
+ zap.String("reason", reason),
+ zap.String("pipeline_log_id", pipelineLogID),
+ zap.String("task_id", taskID),
+ )
return err
}
-// AggregateTaskProgress returns the SQL-aggregated component progress for a
-// task (done/failed/running/percent against the given total denominator).
-func (s *IngestionTaskService) AggregateTaskProgress(ctx context.Context, taskID string, total int) (*dao.TaskProgress, error) {
- return s.ingestionTaskLogDAO.AggregateProgress(ctx, dao.DB, taskID, total)
+func (s *IngestionTaskService) insertEvent(ctx context.Context, kind ingestionEventKind, pipelineLogID, taskID, component string, phase int, message string) error {
+ if pipelineLogID == "" {
+ return rejectIngestionEvent("missing_pipeline_log_id", pipelineLogID, taskID, errors.New("ingestion event requires a pipeline log id"))
+ }
+ if taskID == "" {
+ return rejectIngestionEvent("missing_task_id", pipelineLogID, taskID, errors.New("ingestion event requires a task id"))
+ }
+ eventType := 0
+ switch kind {
+ case ingestionEventLifecycle:
+ eventType = dao.EventTypeLifecycle
+ case ingestionEventMessage:
+ eventType = dao.EventTypeMessage
+ case ingestionEventTerminal:
+ eventType = dao.EventTypeTerminal
+ case ingestionEventSystem:
+ eventType = dao.EventTypeSystem
+ default:
+ return rejectIngestionEvent("invalid_kind", pipelineLogID, taskID, errors.New("ingestion event has invalid kind"))
+ }
+ if kind != ingestionEventLifecycle {
+ component = ""
+ phase = 0
+ }
+ message = s.truncateIngestionEventMessage(message)
+ now := time.Now().Local()
+ // EventTypeLifecycle is zero while the database default is the defensive
+ // legacy value. A map keeps the explicitly mapped protocol value intact;
+ // GORM otherwise substitutes a default-tag value for a zero struct field.
+ return dao.DB.WithContext(ctx).Model(&entity.IngestionTaskLog{}).Create(map[string]interface{}{
+ "task_id": taskID,
+ "pipeline_log_id": pipelineLogID,
+ "checkpoint": entity.JSONMap{},
+ "phase": phase,
+ "event_type": eventType,
+ "component": component,
+ "message": message,
+ "create_time": now.UnixMilli(),
+ "create_date": now.Truncate(time.Second),
+ "update_time": now.UnixMilli(),
+ "update_date": now.Truncate(time.Second),
+ }).Error
}
-// lastRunCount scans all task logs (newest first) for a run_count entry,
-// skipping component-progress rows whose Checkpoint is empty. It returns
-// the counter and whether one was found.
-func (s *IngestionTaskService) lastRunCount(ctx context.Context, taskID string) (int, bool) {
- logs, err := s.ingestionTaskLogDAO.ListLogsByTaskID(ctx, dao.DB, taskID)
- if err != nil {
- return 0, false
+// truncateIngestionEventMessage enforces both limits on the persisted text.
+// The marker is part of the limit, and the dropped count is measured in runes
+// so the result never splits a UTF-8 sequence or misreports multibyte text.
+func (s *IngestionTaskService) truncateIngestionEventMessage(message string) string {
+ limits := s.logSettings
+ if limits.MaxMessageChars <= 0 || limits.MaxMessageBytes <= 0 {
+ limits = DefaultIngestionLogSettings()
}
- for i := len(logs) - 1; i >= 0; i-- {
- if count, ok := common.GetInt(logs[i].Checkpoint[stepKeyRunCount]); ok {
- return count, true
- }
+ if len([]rune(message)) <= limits.MaxMessageChars && len([]byte(message)) <= limits.MaxMessageBytes {
+ return message
}
- return 0, false
-}
-// IncrementRunCount scans existing task logs for the previous run_count
-// (skipping component-progress rows that have no run_count), then INSERTS a
-// new row with the bumped counter. This avoids the race where the latest log
-// is a component-progress row whose empty Checkpoint would cause a parse
-// failure. ListAllForAdmin reads run_count back to render the attempt number.
-//
-// A corrupted run_count value in an existing row is skipped (the row is
-// ignored). A failure to persist the new row is returned so the caller can
-// fail the task before running the pipeline.
-func (s *IngestionTaskService) IncrementRunCount(ctx context.Context, taskID string) error {
- prevCount, _ := s.lastRunCount(ctx, taskID)
+ runes := []rune(message)
+ prefixLen := len(runes)
+ if prefixLen > limits.MaxMessageChars {
+ prefixLen = limits.MaxMessageChars
+ }
+ for prefixLen >= 0 {
+ dropped := len(runes) - prefixLen
+ marker := fmt.Sprintf("… [truncated, %d chars dropped]", dropped)
+ candidate := string(runes[:prefixLen]) + marker
+ if len([]rune(candidate)) <= limits.MaxMessageChars && len([]byte(candidate)) <= limits.MaxMessageBytes {
+ return candidate
+ }
+ prefixLen--
+ }
- entry := &entity.IngestionTaskLog{
- TaskID: taskID,
- Checkpoint: entity.JSONMap{stepKeyRunCount: prevCount + 1},
+ // The marker is tiny relative to the configured limits for any practical
+ // input. Keep a defensive fallback for an unexpectedly huge rune count.
+ marker := fmt.Sprintf("… [truncated, %d chars dropped]", len(runes))
+ if len([]rune(marker)) > limits.MaxMessageChars {
+ return string([]rune(marker)[:limits.MaxMessageChars])
}
- return s.ingestionTaskLogDAO.Create(ctx, dao.DB, entry)
+ return marker
+}
+
+// AggregateTaskProgressByPipelineLogID returns component progress for one
+// immutable ingestion run.
+func (s *IngestionTaskService) AggregateTaskProgressByPipelineLogID(ctx context.Context, pipelineLogID string, total int) (*dao.TaskProgress, error) {
+ return s.ingestionTaskLogDAO.AggregateProgressByPipelineLogID(ctx, dao.DB, pipelineLogID, total)
}
diff --git a/internal/service/ingestion_task_service_test.go b/internal/service/ingestion_task_service_test.go
index 47cbaf414b..77e9b71bc4 100644
--- a/internal/service/ingestion_task_service_test.go
+++ b/internal/service/ingestion_task_service_test.go
@@ -6,6 +6,7 @@ import (
"strings"
"sync"
"testing"
+ "unicode/utf8"
"gorm.io/gorm"
@@ -105,7 +106,7 @@ func TestIngestionTaskServiceCreateForDocumentsPublishesTaskMessages(t *testing.
}
}
-func TestIngestionTaskServiceCreateForDocumentsQueuesWithoutKnowledgebase(t *testing.T) {
+func TestIngestionTaskServiceCreateForDocumentsRejectsMissingRunMetadata(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
insertTestDoc(t, "doc-1", "missing-kb", 0, 0)
@@ -116,20 +117,20 @@ func TestIngestionTaskServiceCreateForDocumentsQueuesWithoutKnowledgebase(t *tes
responses, err := svc.CreateForDocuments(t.Context(), "missing-kb", "user-1", []string{"doc-1"})
if err != nil {
- t.Fatalf("CreateForDocuments should keep early-log lookup best-effort: %v", err)
+ t.Fatalf("CreateForDocuments returns per-document failures: %v", err)
}
- if len(responses) != 1 || !strings.HasPrefix(responses[0].Result, "task_id:") {
+ if len(responses) != 1 || !strings.Contains(responses[0].Result, "ensure run identity") {
t.Fatalf("unexpected responses: %+v", responses)
}
- if len(publisher.messages) != 1 {
- t.Fatalf("published messages = %d, want 1", len(publisher.messages))
+ if len(publisher.messages) != 0 {
+ t.Fatalf("published messages = %d, want 0", len(publisher.messages))
}
task, err := dao.NewIngestionTaskDAO().GetByDocumentID(t.Context(), db, "doc-1")
if err != nil {
t.Fatalf("load queued task: %v", err)
}
- if task == nil || task.Status != common.SCHEDULED {
- t.Fatalf("queued task = %+v, want SCHEDULED", task)
+ if task != nil {
+ t.Fatalf("task = %+v, want no task after identity setup fails", task)
}
if got := countPipelineLogs(t, db, "doc-1"); got != 0 {
t.Fatalf("pipeline log rows = %d, want 0 when metadata lookup fails", got)
@@ -216,7 +217,7 @@ func TestIngestionTaskServiceStartRunningTransitionsScheduledTask(t *testing.T)
insertTestDoc(t, "doc-1", "kb-1", 0, 0)
insertTestIngestionTaskWithStatus(t, "task-1", "user-1", "doc-1", "kb-1", common.SCHEDULED)
- task, err := NewIngestionTaskService().StartRunning(t.Context(), "task-1")
+ task, err := NewIngestionTaskService().TransitionTaskToRunning(t.Context(), "task-1")
if err != nil {
t.Fatalf("StartRunning failed: %v", err)
}
@@ -232,7 +233,7 @@ func TestIngestionTaskServiceStartRunningFromCreatedTask(t *testing.T) {
insertTestDoc(t, "doc-1", "kb-1", 0, 0)
insertTestIngestionTaskWithStatus(t, "task-1", "user-1", "doc-1", "kb-1", common.CREATED)
- task, err := NewIngestionTaskService().StartRunning(t.Context(), "task-1")
+ task, err := NewIngestionTaskService().TransitionTaskToRunning(t.Context(), "task-1")
if err != nil {
t.Fatalf("StartRunning failed: %v", err)
}
@@ -383,10 +384,24 @@ func TestIngestionTaskServiceListAllForAdminIncludesRunAndUserEmail(t *testing.T
t.Fatalf("insert user: %v", err)
}
insertTestIngestionTask(t, "task-1", "user-1", "doc-1", "kb-1")
- if err := dao.DB.Create(&entity.IngestionTaskLog{
- TaskID: "task-1",
- Checkpoint: entity.JSONMap{"run_count": 3},
+ runCount := 3
+ if err := dao.DB.Create(&entity.PipelineOperationLog{
+ ID: "run-1",
+ DocumentID: "doc-1",
+ TenantID: "tenant-1",
+ KbID: "kb-1",
+ TaskType: string(entity.PipelineTaskTypeParse),
+ OperationStatus: string(entity.TaskStatusRunning),
+ RunCount: &runCount,
}).Error; err != nil {
+ t.Fatalf("insert pipeline operation log: %v", err)
+ }
+ if err := dao.DB.Model(&entity.IngestionTask{}).Where("id = ?", "task-1").Update("pipeline_log_id", "run-1").Error; err != nil {
+ t.Fatalf("bind pipeline operation log: %v", err)
+ }
+ if err := dao.DB.Exec(`INSERT INTO ingestion_task_log
+ (task_id, pipeline_log_id, checkpoint, event_type, component, phase, message)
+ VALUES (?, ?, '{}', ?, ?, ?, ?)`, "task-1", "run-1", dao.EventTypeLifecycle, "Parser", 1, "Parser Done").Error; err != nil {
t.Fatalf("insert task log: %v", err)
}
@@ -420,7 +435,7 @@ func TestIngestionTaskServiceStartRunningTransitionsCreatedTask(t *testing.T) {
svc := NewIngestionTaskService()
ctx := t.Context()
- task, err := svc.StartRunning(ctx, "task-1")
+ task, err := svc.TransitionTaskToRunning(ctx, "task-1")
if err != nil {
t.Fatalf("StartRunning failed: %v", err)
}
@@ -429,11 +444,9 @@ func TestIngestionTaskServiceStartRunningTransitionsCreatedTask(t *testing.T) {
}
}
-// TestStartRunningResetsDocumentProgress locks in that starting a CREATED task
-// resets its document progress counters, with a fresh process_begin_at. The
-// document bookkeeping is owned by the task-lifecycle transition, not the
-// ingestion worker's execution path.
-func TestStartRunningResetsDocumentProgress(t *testing.T) {
+// TestPrepareValidatedRunResetsDocumentProgress verifies validation precedes
+// document initialization and leaves the legacy progress_msg untouched.
+func TestPrepareValidatedRunResetsDocumentProgress(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
@@ -451,9 +464,11 @@ func TestStartRunningResetsDocumentProgress(t *testing.T) {
svc := NewIngestionTaskService()
ctx := t.Context()
- if _, err := svc.StartRunning(ctx, "task-1"); err != nil {
- t.Fatalf("StartRunning failed: %v", err)
+ task, err := svc.TransitionTaskToRunning(ctx, "task-1")
+ if err != nil {
+ t.Fatalf("TransitionTaskToRunning failed: %v", err)
}
+ svc.PrepareValidatedRun(ctx, task)
var doc entity.Document
if err := db.Where("id = ?", "doc-1").First(&doc).Error; err != nil {
@@ -468,8 +483,8 @@ func TestStartRunningResetsDocumentProgress(t *testing.T) {
if doc.TokenNum != 0 {
t.Fatalf("token_num = %d, want 0", doc.TokenNum)
}
- if doc.ProgressMsg != nil && *doc.ProgressMsg != "" {
- t.Fatalf("progress_msg = %q, want empty", *doc.ProgressMsg)
+ if doc.ProgressMsg == nil || *doc.ProgressMsg != "partial" {
+ t.Fatalf("progress_msg = %v, want the legacy value unchanged", doc.ProgressMsg)
}
if doc.ProcessBeginAt == nil || doc.ProcessBeginAt.IsZero() {
t.Fatal("process_begin_at not set")
@@ -500,7 +515,7 @@ func TestStartRunningLeavesTerminalDocumentUntouched(t *testing.T) {
svc := NewIngestionTaskService()
ctx := t.Context()
- task, err := svc.StartRunning(ctx, "task-1")
+ task, err := svc.TransitionTaskToRunning(ctx, "task-1")
if err != nil {
t.Fatalf("StartRunning failed: %v", err)
}
@@ -535,7 +550,7 @@ func TestStartRunningFinalizesStoppingTask(t *testing.T) {
svc := NewIngestionTaskService()
ctx := t.Context()
- task, err := svc.StartRunning(ctx, "task-1")
+ task, err := svc.TransitionTaskToRunning(ctx, "task-1")
if err != nil {
t.Fatalf("StartRunning failed: %v", err)
}
@@ -683,6 +698,9 @@ func TestIngestionTaskServiceMarkCompletedReturnsTaskIDInTransitionError(t *test
func TestIngestionTaskServiceCreateAndEnqueueRetriesTerminalTask(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
+ insertTestDoc(t, "doc-2", "kb-1", 0, 0)
publisher := &recordingTaskPublisher{}
svc := NewIngestionTaskService()
svc.taskPublisher = publisher
@@ -690,9 +708,10 @@ func TestIngestionTaskServiceCreateAndEnqueueRetriesTerminalTask(t *testing.T) {
testCases := []struct {
name string
status string
+ docID string
}{
- {name: "failed", status: common.FAILED},
- {name: "stopped", status: common.STOPPED},
+ {name: "failed", status: common.FAILED, docID: "doc-1"},
+ {name: "stopped", status: common.STOPPED, docID: "doc-2"},
}
ctx := t.Context()
@@ -703,13 +722,13 @@ func TestIngestionTaskServiceCreateAndEnqueueRetriesTerminalTask(t *testing.T) {
if err := dao.DB.Where("id = ?", "task-1").Delete(&entity.IngestionTask{}).Error; err != nil {
t.Fatalf("clear task: %v", err)
}
- insertTestIngestionTask(t, "task-1", "user-1", "doc-1", "kb-1")
+ insertTestIngestionTask(t, "task-1", "user-1", tc.docID, "kb-1")
if err := dao.DB.Model(&entity.IngestionTask{}).Where("id = ?", "task-1").Update("status", tc.status).Error; err != nil {
t.Fatalf("set terminal status: %v", err)
}
task, err := svc.CreateAndEnqueue(ctx, &entity.IngestionTask{
- DocumentID: "doc-1",
+ DocumentID: tc.docID,
UserID: "user-1",
DatasetID: "kb-1",
Status: common.CREATED,
@@ -740,6 +759,8 @@ func TestIngestionTaskServiceCreateAndEnqueueRetriesTerminalTask(t *testing.T) {
func TestIngestionTaskServiceCreateAndEnqueueSchedulesExistingCreatedTask(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
insertTestIngestionTaskWithStatus(t, "stale-task", "user-1", "doc-1", "kb-1", common.CREATED)
publisher := &recordingTaskPublisher{}
@@ -777,6 +798,8 @@ func TestIngestionTaskServiceCreateAndEnqueueSchedulesExistingCreatedTask(t *tes
func TestIngestionTaskServiceConcurrentCreatedTaskPublicationsConverge(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
insertTestIngestionTaskWithStatus(t, "task-1", "user-1", "doc-1", "kb-1", common.CREATED)
publisher := &duplicatePublishRecorder{
@@ -843,6 +866,8 @@ func TestIngestionTaskServiceConcurrentCreatedTaskPublicationsConverge(t *testin
func TestIngestionTaskServiceScheduleCreatedTasksKeepsTaskCreatedOnPublishFailure(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
insertTestIngestionTaskWithStatus(t, "task-1", "user-1", "doc-1", "kb-1", common.CREATED)
svc := NewIngestionTaskService()
@@ -855,14 +880,17 @@ func TestIngestionTaskServiceScheduleCreatedTasksKeepsTaskCreatedOnPublishFailur
if err != nil {
t.Fatalf("reload task: %v", err)
}
- if task.Status != common.CREATED {
- t.Fatalf("status after failed recovery publish = %q, want %q", task.Status, common.CREATED)
+ if task.Status != common.FAILED {
+ t.Fatalf("status after failed recovery publish = %q, want %q", task.Status, common.FAILED)
}
}
func TestIngestionTaskServiceScheduleCreatedTasksContinuesAfterPublishFailure(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
+ insertTestDoc(t, "doc-2", "kb-1", 0, 0)
insertTestIngestionTaskWithStatus(t, "task-1", "user-1", "doc-1", "kb-1", common.CREATED)
insertTestIngestionTaskWithStatus(t, "task-2", "user-1", "doc-2", "kb-1", common.CREATED)
if err := db.Model(&entity.IngestionTask{}).Where("id = ?", "task-1").Update("create_time", 1).Error; err != nil {
@@ -886,8 +914,8 @@ func TestIngestionTaskServiceScheduleCreatedTasksContinuesAfterPublishFailure(t
if err != nil {
t.Fatalf("reload first task: %v", err)
}
- if first.Status != common.CREATED {
- t.Fatalf("first task status = %q, want %q", first.Status, common.CREATED)
+ if first.Status != common.FAILED {
+ t.Fatalf("first task status = %q, want %q", first.Status, common.FAILED)
}
second, err := dao.NewIngestionTaskDAO().GetByID(t.Context(), db, "task-2")
if err != nil {
@@ -916,7 +944,7 @@ func TestIngestionTaskServiceCreateAndEnqueueRejectsActiveExistingTask(t *testin
}
}
-func TestIngestionTaskServiceCreateAndEnqueueRollsBackNewTaskOnPublishFailure(t *testing.T) {
+func TestIngestionTaskServiceCreateAndEnqueueSettlesNewRunOnPublishFailure(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
@@ -939,19 +967,19 @@ func TestIngestionTaskServiceCreateAndEnqueueRollsBackNewTaskOnPublishFailure(t
if getErr != nil {
t.Fatalf("reload task by document id: %v", getErr)
}
- if task != nil {
- t.Fatalf("expected created task to be deleted after publish failure, got %+v", task)
+ if task == nil || task.Status != common.FAILED {
+ t.Fatalf("task = %+v, want a durably failed task", task)
}
- open, openErr := dao.NewPipelineOperationLogDAO().GetOpenLogByDocumentID(ctx, db, "doc-1")
- if openErr != nil {
- t.Fatalf("load open pipeline log: %v", openErr)
+ var run entity.PipelineOperationLog
+ if err := db.Where("id = ?", *task.PipelineLogID).First(&run).Error; err != nil {
+ t.Fatalf("load numbered pipeline log: %v", err)
}
- if open != nil {
- t.Fatalf("expected open pipeline log to be deleted after publish failure, got %+v", open)
+ if run.RunCount == nil || *run.RunCount != 1 || run.OperationStatus != string(entity.TaskStatusFail) {
+ t.Fatalf("run = %+v, want numbered failed run", run)
}
}
-func TestIngestionTaskServiceCreateAndEnqueueRollsBackRetriedTaskOnPublishFailure(t *testing.T) {
+func TestIngestionTaskServiceCreateAndEnqueueSettlesRetriedRunOnPublishFailure(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
// Seed the document and KB so buildOpenLogInput succeeds: without them no
@@ -984,60 +1012,61 @@ func TestIngestionTaskServiceCreateAndEnqueueRollsBackRetriedTaskOnPublishFailur
if reloaded.Status != common.FAILED {
t.Fatalf("status = %q, want %q", reloaded.Status, common.FAILED)
}
- // The retry opened an open row before publishing; the failed publish must
- // take it back with the rolled-back task.
- if got := countPipelineLogs(t, db, "doc-1"); got != 0 {
- t.Fatalf("pipeline log rows = %d, want 0 after the retry rollback", got)
+ if reloaded.PipelineLogID == nil {
+ t.Fatal("failed retry lost its run identity")
+ }
+ var run entity.PipelineOperationLog
+ if err := db.Where("id = ?", *reloaded.PipelineLogID).First(&run).Error; err != nil {
+ t.Fatalf("load failed retry run: %v", err)
+ }
+ if run.RunCount == nil || *run.RunCount != 1 || run.OperationStatus != string(entity.TaskStatusFail) {
+ t.Fatalf("run = %+v, want numbered failed retry", run)
}
}
-func TestIngestionTaskServiceRemoveDeletesOwnedTask(t *testing.T) {
+func TestIngestionTaskServiceRemoveSettlesNumberedQueuedRun(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
insertTestDoc(t, "doc-1", "kb-1", 0, 0)
- insertTestIngestionTask(t, "task-1", "user-1", "doc-1", "kb-1")
- ctx := t.Context()
+ insertTestIngestionTaskWithStatus(t, "task-1", "user-1", "doc-1", "kb-1", common.CREATED)
- svc := NewIngestionTaskService()
- queuedMsg := "Task is queued..."
- openLog := &entity.PipelineOperationLog{
- ID: "open-log",
+ runCount := 1
+ if err := db.Create(&entity.PipelineOperationLog{
+ ID: "numbered-open-log",
DocumentID: "doc-1",
TenantID: "tenant-1",
KbID: "kb-1",
ParserID: "naive",
- TaskType: "Parse",
+ TaskType: string(entity.PipelineTaskTypeParse),
OperationStatus: string(entity.TaskStatusUnstart),
- ProgressMsg: &queuedMsg,
- }
- if err := dao.DB.Create(openLog).Error; err != nil {
- t.Fatalf("seed open log: %v", err)
+ RunCount: &runCount,
+ }).Error; err != nil {
+ t.Fatalf("seed numbered pipeline log: %v", err)
}
- // Removal cleanup is scoped to the row the task owns, as bound in
- // production by createOpenLogBestEffort.
- if err := dao.DB.Model(&entity.IngestionTask{}).Where("id = ?", "task-1").
- Update("pipeline_log_id", "open-log").Error; err != nil {
- t.Fatalf("bind open log: %v", err)
+ if err := db.Model(&entity.IngestionTask{}).Where("id = ?", "task-1").
+ Update("pipeline_log_id", "numbered-open-log").Error; err != nil {
+ t.Fatalf("bind numbered pipeline log: %v", err)
}
- userID := "user-1"
- info, err := svc.Remove(ctx, "task-1", &userID)
- if err != nil {
+ if _, err := NewIngestionTaskService().Remove(t.Context(), "task-1", sptr("user-1")); err != nil {
t.Fatalf("Remove failed: %v", err)
}
- if info == nil || info.TaskID != "task-1" {
- t.Fatalf("unexpected task info: %+v", info)
+
+ var run entity.PipelineOperationLog
+ if err := db.First(&run, "id = ?", "numbered-open-log").Error; err != nil {
+ t.Fatalf("numbered run was deleted: %v", err)
}
- if _, err = dao.NewIngestionTaskDAO().GetByID(ctx, db, "task-1"); err == nil {
- t.Fatal("task should be removed")
+ if run.OperationStatus != string(entity.TaskStatusCancel) {
+ t.Fatalf("numbered run status = %q, want %q", run.OperationStatus, entity.TaskStatusCancel)
}
- open, err := dao.NewPipelineOperationLogDAO().GetOpenLogByDocumentID(ctx, db, "doc-1")
- if err != nil {
- t.Fatalf("load open pipeline log: %v", err)
+ var terminal entity.IngestionTaskLog
+ if err := db.Where("pipeline_log_id = ? AND event_type = ?", "numbered-open-log", dao.EventTypeTerminal).
+ First(&terminal).Error; err != nil {
+ t.Fatalf("terminal event missing: %v", err)
}
- if open != nil {
- t.Fatalf("expected open row to be deleted with the task, got %+v", open)
+ if terminal.Message != "Task superseded by a new parse request." {
+ t.Fatalf("terminal message = %q, want supersede reason", terminal.Message)
}
}
@@ -1060,48 +1089,50 @@ func TestIngestionTaskServiceUpdateComponentTotalPersistsDenominator(t *testing.
}
}
-func TestIngestionTaskServiceRecordComponentProgressAppendsRow(t *testing.T) {
+func TestIngestionTaskServiceRecordLifecyclePersistsRunScopedLifecycleEvent(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
insertTestIngestionTask(t, "task-1", "user-1", "doc-1", "kb-1")
- ctx := t.Context()
svc := NewIngestionTaskService()
- if err := svc.RecordComponentProgress(ctx, "task-1", "Parser", 1, "Parser Done"); err != nil {
- t.Fatalf("RecordComponentProgress failed: %v", err)
+ if err := svc.RecordLifecycle(t.Context(), "run-1", "task-1", "Parser", 1, "Parser Done"); err != nil {
+ t.Fatalf("RecordLifecycle failed: %v", err)
}
- logs, err := dao.NewIngestionTaskLogDAO().ListLogsByTaskID(ctx, db, "task-1")
+ logs, err := dao.NewIngestionTaskLogDAO().ListLogsByPipelineLogID(t.Context(), db, "run-1")
if err != nil {
t.Fatalf("list logs: %v", err)
}
if len(logs) != 1 {
- t.Fatalf("expected 1 log row, got %d", len(logs))
+ t.Fatalf("event count = %d, want 1", len(logs))
}
- row := logs[0]
- if row.Component != "Parser" || row.Phase != 1 || row.Message != "Parser Done" {
- t.Fatalf("unexpected log row: %+v", row)
+ event := logs[0]
+ if event.PipelineLogID == nil || *event.PipelineLogID != "run-1" {
+ t.Fatalf("pipeline_log_id = %v, want run-1", event.PipelineLogID)
}
- if len(row.Checkpoint) != 0 {
- t.Fatalf("component progress row must have empty checkpoint, got %v", row.Checkpoint)
+ if event.EventType != dao.EventTypeLifecycle || event.Component != "Parser" || event.Phase != 1 {
+ t.Fatalf("event = %+v, want lifecycle Parser/1", event)
}
}
-func TestIngestionTaskServiceAggregateTaskProgressClassifiesByPhase(t *testing.T) {
+func TestIngestionTaskServiceAggregateTaskProgressByRunClassifiesByPhase(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
insertTestIngestionTask(t, "task-1", "user-1", "doc-1", "kb-1")
ctx := t.Context()
svc := NewIngestionTaskService()
- if err := svc.RecordComponentProgress(ctx, "task-1", "Parser", 1, "Parser Done"); err != nil {
+ if err := svc.RecordLifecycle(ctx, "run-1", "task-1", "Parser", 1, "Parser Done"); err != nil {
t.Fatalf("record Parser: %v", err)
}
- if err := svc.RecordComponentProgress(ctx, "task-1", "Chunker", 0, "Chunker Started"); err != nil {
+ if err := svc.RecordLifecycle(ctx, "run-1", "task-1", "Chunker", 0, "Chunker Started"); err != nil {
t.Fatalf("record Chunker: %v", err)
}
- agg, err := svc.AggregateTaskProgress(ctx, "task-1", 2)
+ if err := svc.RecordLifecycle(ctx, "run-2", "task-1", "Parser", 0, "other run"); err != nil {
+ t.Fatalf("record other run: %v", err)
+ }
+ agg, err := svc.AggregateTaskProgressByPipelineLogID(ctx, "run-1", 2)
if err != nil {
- t.Fatalf("AggregateTaskProgress failed: %v", err)
+ t.Fatalf("AggregateTaskProgressByPipelineLogID failed: %v", err)
}
if agg.Done != 1 || agg.Running != 1 || agg.Failed != 0 {
t.Fatalf("aggregate = %+v, want Done=1 Running=1 Failed=0", agg)
@@ -1111,156 +1142,140 @@ func TestIngestionTaskServiceAggregateTaskProgressClassifiesByPhase(t *testing.T
}
}
-func TestIngestionTaskServiceIncrementRunCountInitializesAndBumps(t *testing.T) {
+func TestIngestionTaskServiceRecordMessageClearsLifecycleFields(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
insertTestIngestionTask(t, "task-1", "user-1", "doc-1", "kb-1")
- ctx := t.Context()
- svc := NewIngestionTaskService()
- if err := svc.IncrementRunCount(ctx, "task-1"); err != nil {
- t.Fatalf("IncrementRunCount (first call) failed: %v", err)
- }
- run, ok := svc.lastRunCount(ctx, "task-1")
- if !ok || run != 1 {
- t.Fatalf("run_count = %v (ok=%v), want 1", run, ok)
+ if err := NewIngestionTaskService().RecordMessage(t.Context(), "run-1", "task-1", "detail"); err != nil {
+ t.Fatalf("RecordMessage failed: %v", err)
}
- // Second call bumps the existing counter to 2.
- if err := svc.IncrementRunCount(ctx, "task-1"); err != nil {
- t.Fatalf("IncrementRunCount (second call) failed: %v", err)
+ var event entity.IngestionTaskLog
+ if err := db.Order("id DESC").First(&event).Error; err != nil {
+ t.Fatalf("load event: %v", err)
}
- run, _ = svc.lastRunCount(ctx, "task-1")
- if run != 2 {
- t.Fatalf("run_count after second bump = %v, want 2", run)
+ if event.EventType != dao.EventTypeMessage || event.Component != "" || event.Phase != 0 || event.Message != "detail" {
+ t.Fatalf("event = %+v, want message with empty component and phase", event)
}
}
-func TestIngestionTaskServiceIncrementRunCountSkippedCorruptedRunCount(t *testing.T) {
- db := setupServiceTestDB(t)
- pushServiceDB(t, db)
- insertTestIngestionTask(t, "task-1", "user-1", "doc-1", "kb-1")
- if err := dao.DB.Create(&entity.IngestionTaskLog{
- TaskID: "task-1",
- Checkpoint: entity.JSONMap{"run_count": "not-a-number"},
- }).Error; err != nil {
- t.Fatalf("insert bad task log: %v", err)
- }
- ctx := t.Context()
-
+func TestIngestionTaskServiceRecordEventRejectsInvalidIdentityAndPhase(t *testing.T) {
svc := NewIngestionTaskService()
- // Corrupted value is skipped; a fresh run_count=1 row is created.
- if err := svc.IncrementRunCount(ctx, "task-1"); err != nil {
- t.Fatalf("IncrementRunCount should skip corrupted value, got: %v", err)
- }
- run, ok := svc.lastRunCount(ctx, "task-1")
- if !ok || run != 1 {
- t.Fatalf("run_count = %v (ok=%v), want 1", run, ok)
+ cases := []struct {
+ name string
+ call func() error
+ }{
+ {name: "missing pipeline log id", call: func() error {
+ return svc.RecordMessage(t.Context(), "", "task-1", "detail")
+ }},
+ {name: "missing task id", call: func() error {
+ return svc.RecordMessage(t.Context(), "run-1", "", "detail")
+ }},
+ {name: "missing lifecycle component", call: func() error {
+ return svc.RecordLifecycle(t.Context(), "run-1", "task-1", "", 0, "started")
+ }},
+ {name: "invalid lifecycle phase", call: func() error {
+ return svc.RecordLifecycle(t.Context(), "run-1", "task-1", "Parser", 3, "bad")
+ }},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ if err := tc.call(); err == nil {
+ t.Fatal("expected event validation error")
+ }
+ })
}
}
-func TestIngestionTaskServiceIncrementRunCountRecoversFromComponentProgressLog(t *testing.T) {
+func TestIngestionTaskServiceEventMessageIsBoundedByRunesAndBytes(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
insertTestIngestionTask(t, "task-1", "user-1", "doc-1", "kb-1")
- ctx := t.Context()
- // Simulate a previous run that created some component-progress logs
- // but died before recording a run_count row. The latest log has no run_count.
- svc := NewIngestionTaskService()
- if err := svc.RecordComponentProgress(ctx, "task-1", "Parser", 1, "Parser Done"); err != nil {
- t.Fatalf("record Parser: %v", err)
- }
- if err := svc.RecordComponentProgress(ctx, "task-1", "Chunker", 1, "Chunker Done"); err != nil {
- t.Fatalf("record Chunker: %v", err)
- }
- // Verify latest log has empty checkpoint (no run_count).
- latest, err := dao.NewIngestionTaskLogDAO().LatestLogByTaskID(ctx, db, "task-1")
- if err != nil {
- t.Fatalf("load latest: %v", err)
- }
- if len(latest.Checkpoint) != 0 {
- t.Fatalf("component-progress row should have empty checkpoint, got %v", latest.Checkpoint)
+ message := strings.Repeat("界", 10_000)
+ if err := NewIngestionTaskService().RecordMessage(t.Context(), "run-1", "task-1", message); err != nil {
+ t.Fatalf("RecordMessage failed: %v", err)
}
- // IncrementRunCount should create a new row with run_count=1.
- if err = svc.IncrementRunCount(ctx, "task-1"); err != nil {
- t.Fatalf("IncrementRunCount failed: %v", err)
+ var event entity.IngestionTaskLog
+ if err := db.Order("id DESC").First(&event).Error; err != nil {
+ t.Fatalf("load event: %v", err)
}
- run, ok := svc.lastRunCount(ctx, "task-1")
- if !ok || run != 1 {
- t.Fatalf("run_count = %v (ok=%v), want 1", run, ok)
+ if got := len([]rune(event.Message)); got > 4_000 {
+ t.Fatalf("message rune length = %d, want <= 4000", got)
}
-
- // AggregateProgress should still work (run_count row with component=""
- // has phase=0, which doesn't affect counts).
- agg, err := svc.AggregateTaskProgress(ctx, "task-1", 2)
- if err != nil {
- t.Fatalf("AggregateTaskProgress: %v", err)
+ if got := len([]byte(event.Message)); got > 16_384 {
+ t.Fatalf("message byte length = %d, want <= 16384", got)
+ }
+ if !strings.Contains(event.Message, "… [truncated,") || !strings.HasSuffix(event.Message, " chars dropped]") {
+ t.Fatalf("message lacks truncation marker: %q", event.Message[len(event.Message)-64:])
}
- if agg.Done != 2 {
- t.Fatalf("Done = %d, want 2 (run_count row didn't interfere)", agg.Done)
+ if !utf8.ValidString(event.Message) {
+ t.Fatal("message is not valid UTF-8")
}
}
-func TestIngestionTaskServiceIncrementRunCountAccumulatesAcrossRetries(t *testing.T) {
+func TestIngestionTaskServiceUsesConfiguredEventMessageLimits(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
insertTestIngestionTask(t, "task-1", "user-1", "doc-1", "kb-1")
svc := NewIngestionTaskService()
- ctx := t.Context()
-
- // First attempt: IncrementRunCount creates run_count=1.
- if err := svc.IncrementRunCount(ctx, "task-1"); err != nil {
- t.Fatalf("first IncrementRunCount: %v", err)
- }
- // Simulate first run: some components progress, then failure.
- if err := svc.RecordComponentProgress(ctx, "task-1", "Parser", 1, "Parser Done"); err != nil {
- t.Fatalf("record Parser: %v", err)
+ if err := svc.SetIngestionLogSettings(IngestionLogSettings{
+ MaxRowsPerRun: 5,
+ MaxRowsPerDocument: 20,
+ MaxMessageChars: 8,
+ MaxMessageBytes: 16,
+ }); err != nil {
+ t.Fatalf("SetIngestionLogSettings failed: %v", err)
}
-
- // Second attempt (retry): should find previous run_count=1 and create row with run_count=2.
- if err := svc.IncrementRunCount(ctx, "task-1"); err != nil {
- t.Fatalf("second IncrementRunCount: %v", err)
- }
- // More progress, then failure.
- if err := svc.RecordComponentProgress(ctx, "task-1", "Chunker", 1, "Chunker Done"); err != nil {
- t.Fatalf("record Chunker: %v", err)
+ if err := svc.RecordMessage(t.Context(), "run-1", "task-1", "abcdefghijk"); err != nil {
+ t.Fatalf("RecordMessage failed: %v", err)
}
- // Third attempt (retry): should find previous run_count=2 and create row with run_count=3.
- if err := svc.IncrementRunCount(ctx, "task-1"); err != nil {
- t.Fatalf("third IncrementRunCount: %v", err)
+ var event entity.IngestionTaskLog
+ if err := db.Order("id DESC").First(&event).Error; err != nil {
+ t.Fatalf("load event: %v", err)
}
-
- run, ok := svc.lastRunCount(ctx, "task-1")
- if !ok || run != 3 {
- t.Fatalf("run_count = %v (ok=%v), want 3", run, ok)
+ if got := len([]rune(event.Message)); got > 8 || len([]byte(event.Message)) > 16 {
+ t.Fatalf("message limits = %d runes/%d bytes, want <= 8/16: %q", got, len([]byte(event.Message)), event.Message)
}
+}
- // ListAllForAdmin should still pick up the correct run_count.
- status := "1"
- if err := dao.DB.Create(&entity.User{
- ID: "user-1",
- Email: "[email protected]",
- Nickname: "user-1",
- IsAuthenticated: "1",
- IsActive: "1",
- IsAnonymous: "0",
- Status: &status,
+func TestIngestionTaskServiceAdvanceOpenLogLeavesLegacyMessageUntouched(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
+ insertTestIngestionTask(t, "task-1", "user-1", "doc-1", "kb-1")
+ legacy := "legacy value"
+ if err := db.Create(&entity.PipelineOperationLog{
+ ID: "run-1",
+ DocumentID: "doc-1",
+ TenantID: "tenant-1",
+ KbID: "kb-1",
+ TaskType: string(entity.PipelineTaskTypeParse),
+ OperationStatus: string(entity.TaskStatusUnstart),
+ ProgressMsg: &legacy,
}).Error; err != nil {
- t.Fatalf("insert user: %v", err)
+ t.Fatalf("insert pipeline operation log: %v", err)
+ }
+ if err := db.Model(&entity.IngestionTask{}).Where("id = ?", "task-1").Update("pipeline_log_id", "run-1").Error; err != nil {
+ t.Fatalf("bind pipeline operation log: %v", err)
}
- adminTasks, err := svc.ListAllForAdmin(ctx)
+ task, err := dao.NewIngestionTaskDAO().GetByID(t.Context(), db, "task-1")
if err != nil {
- t.Fatalf("ListAllForAdmin: %v", err)
+ t.Fatalf("load task: %v", err)
}
- if len(adminTasks) != 1 || adminTasks[0]["id"] != "task-1" {
- t.Fatalf("ListAllForAdmin = %+v, want single task task-1", adminTasks)
+
+ NewIngestionTaskService().advanceOpenLog(t.Context(), task, logFromUnstart, string(entity.TaskStatusSchedule))
+ var run entity.PipelineOperationLog
+ if err := db.First(&run, "id = ?", "run-1").Error; err != nil {
+ t.Fatalf("load run: %v", err)
}
- if adminTasks[0]["run_count"] != 3 {
- t.Fatalf("ListAllForAdmin run_count = %v, want 3", adminTasks[0]["run_count"])
+ if run.ProgressMsg == nil || *run.ProgressMsg != legacy {
+ t.Fatalf("progress_msg = %v, want unchanged legacy value %q", run.ProgressMsg, legacy)
}
}
@@ -1378,14 +1393,15 @@ func TestIngestionTaskServiceMarkCompletedIdempotentOnAlreadyTerminal(t *testing
func loadOpenPipelineLog(t *testing.T, ctx context.Context, db *gorm.DB, documentID string) *entity.PipelineOperationLog {
t.Helper()
- open, err := dao.NewPipelineOperationLogDAO().GetOpenLogByDocumentID(ctx, db, documentID)
- if err != nil {
- t.Fatalf("load open pipeline log: %v", err)
- }
- if open == nil {
+ var open entity.PipelineOperationLog
+ if err := db.WithContext(ctx).
+ Where("document_id = ? AND operation_status IN ?", documentID, dao.OpenPipelineOperationStatuses()).
+ Order("create_time DESC").
+ Order("id DESC").
+ First(&open).Error; err != nil {
t.Fatalf("expected open pipeline log for document %s", documentID)
}
- return open
+ return &open
}
func countPipelineLogs(t *testing.T, db *gorm.DB, documentID string) int {
@@ -1417,8 +1433,25 @@ func TestIngestionTaskServiceCreateForDocumentsOpensPreTerminalPipelineLog(t *te
if open.KbID != "kb-1" || open.TenantID != "tenant-1" {
t.Fatalf("unexpected kb/tenant scope: %+v", open)
}
- if open.ProgressMsg == nil || *open.ProgressMsg != "Task is queued..." {
- t.Fatalf("ProgressMsg = %v, want queued message", open.ProgressMsg)
+ if open.RunCount == nil || *open.RunCount != 1 {
+ t.Fatalf("run_count = %v, want 1", open.RunCount)
+ }
+ if open.ProgressMsg != nil {
+ t.Fatalf("ProgressMsg = %v, want legacy field untouched", open.ProgressMsg)
+ }
+ events, err := dao.NewIngestionTaskLogDAO().ListLogsByPipelineLogID(ctx, db, open.ID)
+ if err != nil {
+ t.Fatalf("list run events: %v", err)
+ }
+ var queuedMessage *entity.IngestionTaskLog
+ for _, event := range events {
+ if event.EventType == dao.EventTypeMessage && event.Message == "Task is queued..." {
+ queuedMessage = event
+ break
+ }
+ }
+ if queuedMessage == nil {
+ t.Fatalf("expected queued message event for run %s, events: %+v", open.ID, events)
}
if countPipelineLogs(t, db, "doc-1") != 1 {
t.Fatalf("expected exactly 1 pipeline log row for one run")
@@ -1471,9 +1504,11 @@ func TestIngestionTaskServiceStartRunningAdvancesPreTerminalPipelineLog(t *testi
if err != nil {
t.Fatalf("reload task: %v", err)
}
- if _, err := svc.StartRunning(ctx, task.ID); err != nil {
- t.Fatalf("StartRunning failed: %v", err)
+ task, err = svc.TransitionTaskToRunning(ctx, task.ID)
+ if err != nil {
+ t.Fatalf("TransitionTaskToRunning failed: %v", err)
}
+ svc.PrepareValidatedRun(ctx, task)
open = loadOpenPipelineLog(t, ctx, db, "doc-1")
if open.OperationStatus != string(entity.TaskStatusRunning) {
t.Fatalf("OperationStatus after start = %q, want %q", open.OperationStatus, string(entity.TaskStatusRunning))
@@ -1564,6 +1599,98 @@ func TestIngestionTaskServiceRetryAfterTerminalOpensFreshPipelineLog(t *testing.
}
}
+func TestIngestionTaskServiceRetryAllocatesNextRunCount(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
+
+ svc := NewIngestionTaskService()
+ svc.taskPublisher = &recordingTaskPublisher{}
+ first, err := svc.CreateAndEnqueue(t.Context(), &entity.IngestionTask{
+ DocumentID: "doc-1", UserID: "user-1", DatasetID: "kb-1", Status: common.CREATED,
+ })
+ if err != nil {
+ t.Fatalf("create first run: %v", err)
+ }
+ if first.PipelineLogID == nil {
+ t.Fatal("first run has no pipeline log id")
+ }
+ svc.settlePublishFailure(t.Context(), first)
+
+ second, err := svc.CreateAndEnqueue(t.Context(), &entity.IngestionTask{
+ DocumentID: "doc-1", UserID: "user-1", DatasetID: "kb-1", Status: common.CREATED,
+ })
+ if err != nil {
+ t.Fatalf("retry: %v", err)
+ }
+ if second.PipelineLogID == nil || *second.PipelineLogID == *first.PipelineLogID {
+ t.Fatalf("second run id = %v, want a new run", second.PipelineLogID)
+ }
+ var secondLog entity.PipelineOperationLog
+ if err := db.First(&secondLog, "id = ?", *second.PipelineLogID).Error; err != nil {
+ t.Fatalf("load second run: %v", err)
+ }
+ if secondLog.RunCount == nil || *secondLog.RunCount != 2 {
+ t.Fatalf("second run_count = %v, want 2", secondLog.RunCount)
+ }
+}
+
+func TestIngestionTaskServiceReloadAndValidateRunIdentityRejectsInvalidBindings(t *testing.T) {
+ validRunCount := 1
+ invalidRunCount := 0
+ testCases := []struct {
+ name string
+ pipelineLogID *string
+ createRun bool
+ runDocumentID string
+ runDatasetID string
+ runCount *int
+ wantReason string
+ }{
+ {name: "missing pipeline log ID", wantReason: "missing_pipeline_log_id"},
+ {name: "pipeline log not found", pipelineLogID: sptr("missing-run"), wantReason: "pipeline_log_not_found"},
+ {name: "pipeline log document mismatch", pipelineLogID: sptr("run-1"), createRun: true, runDocumentID: "other-doc", runDatasetID: "kb-1", runCount: &validRunCount, wantReason: "pipeline_log_document_mismatch"},
+ {name: "pipeline log dataset mismatch", pipelineLogID: sptr("run-1"), createRun: true, runDocumentID: "doc-1", runDatasetID: "other-kb", runCount: &validRunCount, wantReason: "pipeline_log_dataset_mismatch"},
+ {name: "missing run count", pipelineLogID: sptr("run-1"), createRun: true, runDocumentID: "doc-1", runDatasetID: "kb-1", wantReason: "invalid_run_count"},
+ {name: "non-positive run count", pipelineLogID: sptr("run-1"), createRun: true, runDocumentID: "doc-1", runDatasetID: "kb-1", runCount: &invalidRunCount, wantReason: "invalid_run_count"},
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.name, func(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
+ insertTestIngestionTask(t, "task-1", "user-1", "doc-1", "kb-1")
+
+ if testCase.createRun {
+ if err := db.Create(&entity.PipelineOperationLog{
+ ID: *testCase.pipelineLogID,
+ DocumentID: testCase.runDocumentID,
+ KbID: testCase.runDatasetID,
+ TaskType: string(entity.PipelineTaskTypeParse),
+ OperationStatus: string(entity.TaskStatusRunning),
+ RunCount: testCase.runCount,
+ }).Error; err != nil {
+ t.Fatalf("create pipeline operation log: %v", err)
+ }
+ }
+ if testCase.pipelineLogID != nil {
+ if err := db.Model(&entity.IngestionTask{}).Where("id = ?", "task-1").Update("pipeline_log_id", *testCase.pipelineLogID).Error; err != nil {
+ t.Fatalf("bind pipeline operation log: %v", err)
+ }
+ }
+
+ _, err := NewIngestionTaskService().ReloadAndValidateRunIdentity(t.Context(), "task-1")
+ var identityErr *InvalidRunIdentityError
+ if !errors.As(err, &identityErr) || identityErr.Reason != testCase.wantReason {
+ t.Fatalf("error = %v, want invalid run identity reason %q", err, testCase.wantReason)
+ }
+ })
+ }
+}
+
// TestIngestionTaskServiceOpensPreTerminalLogBeforePublish locks the ordering that
// closes the orphan window: the run's row must exist before its message is
// published, so a worker that claims and finishes the task immediately still
@@ -1632,7 +1759,7 @@ func TestIngestionTaskServiceStartRunningClosingStoppingTaskClosesPreTerminalLog
}
svc := NewIngestionTaskService()
- task, err := svc.StartRunning(t.Context(), "task-1")
+ task, err := svc.TransitionTaskToRunning(t.Context(), "task-1")
if err != nil {
t.Fatalf("StartRunning failed: %v", err)
}
@@ -1659,11 +1786,13 @@ func TestIngestionTaskServiceKeepsBoundRowOverUnrelatedOpenRow(t *testing.T) {
insertTestIngestionTaskWithStatus(t, "task-1", "user-1", "doc-1", "kb-1", common.CREATED)
queuedMsg := "Task is queued..."
- seed := func(id, status string) {
+ runCount := 1
+ seed := func(id, status string, runCount *int) {
t.Helper()
if err := dao.DB.Create(&entity.PipelineOperationLog{
ID: id,
DocumentID: "doc-1",
+ RunCount: runCount,
TenantID: "tenant-1",
KbID: "kb-1",
ParserID: "naive",
@@ -1674,8 +1803,8 @@ func TestIngestionTaskServiceKeepsBoundRowOverUnrelatedOpenRow(t *testing.T) {
t.Fatalf("seed %s: %v", id, err)
}
}
- seed("bound-log", string(entity.TaskStatusUnstart))
- seed("unrelated-log", string(entity.TaskStatusSchedule))
+ seed("bound-log", string(entity.TaskStatusUnstart), &runCount)
+ seed("unrelated-log", string(entity.TaskStatusSchedule), nil)
if err := dao.DB.Model(&entity.IngestionTask{}).Where("id = ?", "task-1").
Update("pipeline_log_id", "bound-log").Error; err != nil {
t.Fatalf("bind: %v", err)
@@ -1725,9 +1854,13 @@ func TestIngestionTaskServiceStaleSnapshotKeepsEstablishedOpenLogBinding(t *test
}
svc := NewIngestionTaskService()
- svc.createOpenLogBestEffort(t.Context(), firstSnapshot, string(entity.TaskStatusUnstart), logMsgQueued, nil)
+ if err := svc.ensureRunIdentity(t.Context(), firstSnapshot, nil); err != nil {
+ t.Fatalf("establish first identity: %v", err)
+ }
firstLogID := loadTaskPipelineLogID(t, t.Context(), db, "task-1")
- svc.createOpenLogBestEffort(t.Context(), staleSnapshot, string(entity.TaskStatusUnstart), logMsgQueued, nil)
+ if err := svc.ensureRunIdentity(t.Context(), staleSnapshot, nil); err != nil {
+ t.Fatalf("reuse established identity: %v", err)
+ }
if got := loadTaskPipelineLogID(t, t.Context(), db, "task-1"); got != firstLogID {
t.Fatalf("stale snapshot replaced binding %q with %q", firstLogID, got)
@@ -1741,122 +1874,6 @@ func TestIngestionTaskServiceStaleSnapshotKeepsEstablishedOpenLogBinding(t *test
}
}
-func TestIngestionTaskServiceDoesNotDeleteOpenLogOwnedByActiveTask(t *testing.T) {
- db := setupServiceTestDB(t)
- pushServiceDB(t, db)
- insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
- insertTestDoc(t, "doc-1", "kb-1", 0, 0)
- if err := db.Migrator().DropIndex(&entity.IngestionTask{}, "idx_ingestion_task_document_id"); err != nil {
- t.Fatalf("drop document uniqueness to model an existing database: %v", err)
- }
- insertTestIngestionTaskWithStatus(t, "owner-task", "user-1", "doc-1", "kb-1", common.RUNNING)
- insertTestIngestionTaskWithStatus(t, "new-task", "user-1", "doc-1", "kb-1", common.CREATED)
-
- queuedMsg := logMsgRunning
- ownedLog := &entity.PipelineOperationLog{
- ID: "owned-log",
- DocumentID: "doc-1",
- TenantID: "tenant-1",
- KbID: "kb-1",
- ParserID: "naive",
- TaskType: string(entity.PipelineTaskTypeParse),
- OperationStatus: string(entity.TaskStatusRunning),
- ProgressMsg: &queuedMsg,
- }
- if err := db.Create(ownedLog).Error; err != nil {
- t.Fatalf("seed owned open log: %v", err)
- }
- if err := db.Model(&entity.IngestionTask{}).Where("id = ?", "owner-task").Update("pipeline_log_id", ownedLog.ID).Error; err != nil {
- t.Fatalf("bind owner task: %v", err)
- }
-
- newTask, err := dao.NewIngestionTaskDAO().GetByID(t.Context(), db, "new-task")
- if err != nil {
- t.Fatalf("load new task: %v", err)
- }
- svc := NewIngestionTaskService()
- svc.createOpenLogBestEffort(t.Context(), newTask, string(entity.TaskStatusUnstart), logMsgQueued, nil)
-
- var reloaded entity.PipelineOperationLog
- if err := db.First(&reloaded, "id = ?", ownedLog.ID).Error; err != nil {
- t.Fatalf("active task's open log was deleted: %v", err)
- }
- if got := countPipelineLogs(t, db, "doc-1"); got != 1 {
- t.Fatalf("pipeline log rows = %d, want only the active task's row", got)
- }
- reloadedTask, err := dao.NewIngestionTaskDAO().GetByID(t.Context(), db, "new-task")
- if err != nil {
- t.Fatalf("reload new task: %v", err)
- }
- if reloadedTask.PipelineLogID != nil {
- t.Fatalf("new task bound to %q while another active task owns the open row", *reloadedTask.PipelineLogID)
- }
-}
-
-// TestIngestionTaskServiceNewRunReplacesStaleOpenRow locks the pre-binding
-// window: a brand-new task arrives with no bound row, and an open row left over
-// for the document (a run whose cleanup failed) must not be adopted. Since
-// ingestion_task.document_id is unique, this task is the document's only task,
-// so such a row cannot belong to a live run — the new run must open its own row
-// and the stale one must not linger as a permanently queued entry.
-func TestIngestionTaskServiceNewRunReplacesStaleOpenRow(t *testing.T) {
- db := setupServiceTestDB(t)
- pushServiceDB(t, db)
- insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
- insertTestDoc(t, "doc-1", "kb-1", 0, 0)
-
- // An orphaned row in a pre-terminal state, from a run that no longer owns a
- // task, with stale content the new run must not inherit.
- staleMsg := "Task is running..."
- if err := dao.DB.Create(&entity.PipelineOperationLog{
- ID: "stale-log",
- DocumentID: "doc-1",
- TenantID: "tenant-1",
- KbID: "kb-1",
- ParserID: "naive",
- TaskType: "Parse",
- OperationStatus: string(entity.TaskStatusRunning),
- ProgressMsg: &staleMsg,
- PipelineTitle: strPtr("stale-pipeline"),
- }).Error; err != nil {
- t.Fatalf("seed stale log: %v", err)
- }
-
- svc := NewIngestionTaskService()
- svc.taskPublisher = &recordingTaskPublisher{}
- ctx := t.Context()
- if _, err := svc.CreateForDocuments(ctx, "kb-1", "user-1", []string{"doc-1"}); err != nil {
- t.Fatalf("CreateForDocuments failed: %v", err)
- }
-
- task, err := dao.NewIngestionTaskDAO().GetByDocumentID(ctx, db, "doc-1")
- if err != nil {
- t.Fatalf("reload task: %v", err)
- }
- if task.PipelineLogID == nil {
- t.Fatal("task has no bound open log row")
- }
- if *task.PipelineLogID == "stale-log" {
- t.Fatalf("task adopted the stale row %q; want a fresh row", *task.PipelineLogID)
- }
- if err := db.First(&entity.PipelineOperationLog{}, "id = ?", "stale-log").Error; !dao.IsNotFoundErr(err) {
- t.Fatalf("stale open row survived; want it dropped, got err=%v", err)
- }
- if got := countPipelineLogs(t, db, "doc-1"); got != 1 {
- t.Fatalf("pipeline log rows = %d, want 1 (stale dropped, fresh queued)", got)
- }
- open := loadOpenPipelineLog(t, ctx, db, "doc-1")
- if open.ID != *task.PipelineLogID {
- t.Fatalf("open row %q is not the bound row %q", open.ID, *task.PipelineLogID)
- }
- if open.OperationStatus != string(entity.TaskStatusSchedule) {
- t.Fatalf("OperationStatus = %q, want %q (fresh row, not stale RUNNING)", open.OperationStatus, string(entity.TaskStatusSchedule))
- }
- if open.PipelineTitle != nil && *open.PipelineTitle == "stale-pipeline" {
- t.Fatalf("new run inherited the stale row's content: %+v", open)
- }
-}
-
// TestIngestionTaskServicePreTerminalLogAdvanceIsMonotonic locks the monotonic
// transition contract: a late queued write must not regress a row a concurrent
// worker already moved to running.
@@ -1867,7 +1884,6 @@ func TestIngestionTaskServicePreTerminalLogAdvanceIsMonotonic(t *testing.T) {
insertTestDoc(t, "doc-1", "kb-1", 0, 0)
insertTestIngestionTaskWithStatus(t, "task-1", "user-1", "doc-1", "kb-1", common.CREATED)
- queuedMsg := "Task is queued..."
openLog := &entity.PipelineOperationLog{
ID: "log-1",
DocumentID: "doc-1",
@@ -1876,7 +1892,6 @@ func TestIngestionTaskServicePreTerminalLogAdvanceIsMonotonic(t *testing.T) {
ParserID: "naive",
TaskType: "Parse",
OperationStatus: string(entity.TaskStatusUnstart),
- ProgressMsg: &queuedMsg,
}
if err := dao.DB.Create(openLog).Error; err != nil {
t.Fatalf("seed open log: %v", err)
@@ -1892,16 +1907,16 @@ func TestIngestionTaskServicePreTerminalLogAdvanceIsMonotonic(t *testing.T) {
t.Fatalf("load task: %v", err)
}
- svc.advanceOpenLog(ctx, task, logFromUnstart, string(entity.TaskStatusSchedule), logMsgQueued, true)
+ svc.advanceOpenLog(ctx, task, logFromUnstart, string(entity.TaskStatusSchedule))
if got := loadOpenPipelineLog(t, ctx, db, "doc-1").OperationStatus; got != string(entity.TaskStatusSchedule) {
t.Fatalf("after schedule = %q, want %q", got, string(entity.TaskStatusSchedule))
}
- svc.advanceOpenLog(ctx, task, logFromUnstartOrScheduled, string(entity.TaskStatusRunning), logMsgRunning, true)
+ svc.advanceOpenLog(ctx, task, logFromUnstartOrScheduled, string(entity.TaskStatusRunning))
if got := loadOpenPipelineLog(t, ctx, db, "doc-1").OperationStatus; got != string(entity.TaskStatusRunning) {
t.Fatalf("after start = %q, want %q", got, string(entity.TaskStatusRunning))
}
// The enqueue-time scheduled write lands late; it must not regress the row.
- svc.advanceOpenLog(ctx, task, logFromUnstart, string(entity.TaskStatusSchedule), logMsgQueued, true)
+ svc.advanceOpenLog(ctx, task, logFromUnstart, string(entity.TaskStatusSchedule))
open := loadOpenPipelineLog(t, ctx, db, "doc-1")
if open.OperationStatus != string(entity.TaskStatusRunning) {
t.Fatalf("late scheduled write regressed the row to %q, want %q", open.OperationStatus, string(entity.TaskStatusRunning))
@@ -1911,10 +1926,10 @@ func TestIngestionTaskServicePreTerminalLogAdvanceIsMonotonic(t *testing.T) {
}
}
-// TestIngestionTaskServiceAdvanceReopenGuard locks the reopen contract: a
-// missing open row is re-created only while the task is still live. A terminal
-// task must never resurrect its closed row as a permanently queued entry.
-func TestIngestionTaskServiceAdvanceReopenGuard(t *testing.T) {
+// TestIngestionTaskServiceAdvanceRejectsMissingIdentity ensures a status
+// advance never creates a run identity. Only publish-time EnsureRunIdentity
+// may bind a task to a pipeline-operation-log row.
+func TestIngestionTaskServiceAdvanceRejectsMissingIdentity(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
@@ -1930,7 +1945,7 @@ func TestIngestionTaskServiceAdvanceReopenGuard(t *testing.T) {
if err != nil {
t.Fatalf("load terminal task: %v", err)
}
- svc.advanceOpenLog(ctx, terminal, logFromUnstart, string(entity.TaskStatusSchedule), logMsgQueued, true)
+ svc.advanceOpenLog(ctx, terminal, logFromUnstart, string(entity.TaskStatusSchedule))
if got := countPipelineLogs(t, db, "doc-terminal"); got != 0 {
t.Fatalf("terminal task resurrected %d queued row(s); want none", got)
}
@@ -1939,12 +1954,8 @@ func TestIngestionTaskServiceAdvanceReopenGuard(t *testing.T) {
if err != nil {
t.Fatalf("load live task: %v", err)
}
- svc.advanceOpenLog(ctx, live, logFromUnstart, string(entity.TaskStatusSchedule), logMsgQueued, true)
- open := loadOpenPipelineLog(t, ctx, db, "doc-live")
- if open.OperationStatus != string(entity.TaskStatusSchedule) {
- t.Fatalf("OperationStatus = %q, want %q (reopen for a live task)", open.OperationStatus, string(entity.TaskStatusSchedule))
- }
- if bound := loadTaskPipelineLogID(t, ctx, db, "task-live"); bound != open.ID {
- t.Fatalf("task bound to %q, want reopened row %q", bound, open.ID)
+ svc.advanceOpenLog(ctx, live, logFromUnstart, string(entity.TaskStatusSchedule))
+ if got := countPipelineLogs(t, db, "doc-live"); got != 0 {
+ t.Fatalf("missing identity created %d pipeline log rows, want 0", got)
}
}
diff --git a/web/src/interfaces/database/dataset.ts b/web/src/interfaces/database/dataset.ts
index d70d74c661..e9b144c486 100644
--- a/web/src/interfaces/database/dataset.ts
+++ b/web/src/interfaces/database/dataset.ts
@@ -3,6 +3,7 @@
import { IngestionTaskStatus, RunningStatus } from '@/constants/knowledge';
import { DataSourceKey } from '@/pages/user-setting/data-source/constant';
+import { IngestionEventItem } from './ingestion';
export interface IConnector {
id: string;
@@ -133,6 +134,7 @@ export interface IKnowledgeFile {
process_duration: number;
progress: number; // parsing process
progress_msg: string; // parsing log
+ latest_ingestion_event?: IngestionEventItem | null;
// Python backend only. The Go backend removed this field and reports
// parsing state exclusively through ingestion_status.
run?: RunningStatus; // parsing status
diff --git a/web/src/interfaces/database/document.ts b/web/src/interfaces/database/document.ts
index 16eed6b277..30718b9948 100644
--- a/web/src/interfaces/database/document.ts
+++ b/web/src/interfaces/database/document.ts
@@ -1,4 +1,5 @@
import { IngestionTaskStatus, RunningStatus } from '@/constants/knowledge';
+import { IngestionEventItem } from './ingestion';
export interface IDocumentInfo {
// chunk_num: number;
@@ -18,6 +19,7 @@ export interface IDocumentInfo {
process_duration: number;
progress: number;
progress_msg: string;
+ latest_ingestion_event?: IngestionEventItem | null;
// Python backend only. The Go backend removed this field and reports
// parsing state exclusively through ingestion_status.
run?: RunningStatus;
diff --git a/web/src/interfaces/database/ingestion.ts b/web/src/interfaces/database/ingestion.ts
new file mode 100644
index 0000000000..dd2b55dfbb
--- /dev/null
+++ b/web/src/interfaces/database/ingestion.ts
@@ -0,0 +1,24 @@
+export interface IngestionEventItem {
+ id: number;
+ ts: string;
+ event_type: number;
+ component: string;
+ phase: number;
+ message: string;
+}
+
+export interface IngestionMessagesResponse {
+ run_count: number;
+ items: IngestionEventItem[];
+ oldest_id?: number;
+ newest_id?: number;
+ has_more_before: boolean;
+ has_more_after: boolean;
+ terminal: boolean;
+}
+
+export interface IngestionMessageParams {
+ limit?: number;
+ after_id?: number;
+ before_id?: number;
+}
diff --git a/web/src/pages/dataset/dataset-overview/interface.ts b/web/src/pages/dataset/dataset-overview/interface.ts
index 795b50bbcd..2071740adc 100644
--- a/web/src/pages/dataset/dataset-overview/interface.ts
+++ b/web/src/pages/dataset/dataset-overview/interface.ts
@@ -1,5 +1,6 @@
import { RunningStatus, RunningStatusMap } from '../dataset/constant';
import { LogTabs } from './dataset-common';
+import { IngestionEventItem } from '@/interfaces/database/ingestion';
export interface DocumentLog {
fileName: string;
@@ -49,7 +50,8 @@ export interface IFileLogItem {
process_begin_at: null | string;
process_duration: number;
progress: number;
- progress_msg: string;
+ progress_msg?: string;
+ latest_ingestion_event?: IngestionEventItem | null;
source_type?: string;
source_from?: string;
status: string;
diff --git a/web/src/pages/dataset/dataset-overview/overview-table.tsx b/web/src/pages/dataset/dataset-overview/overview-table.tsx
index d127a60dc9..4e7e089bf3 100644
--- a/web/src/pages/dataset/dataset-overview/overview-table.tsx
+++ b/web/src/pages/dataset/dataset-overview/overview-table.tsx
@@ -26,6 +26,8 @@ import {
import { useTranslate } from '@/hooks/common-hooks';
import { cn } from '@/lib/utils';
import { useDataSourceInfo } from '@/pages/user-setting/data-source/constant';
+import { useGetKnowledgeSearchParams } from '@/hooks/route-hook';
+import { useIsGoBackend } from '@/utils/backend-variant';
import { IDataSourceInfoMap } from '@/pages/user-setting/data-source/interface';
import { formatDate, formatSecondsToHumanReadable } from '@/utils/date';
import {
@@ -42,9 +44,11 @@ import {
} from '@tanstack/react-table';
import { TFunction } from 'i18next';
import { ArrowUpDown, Eye, MonitorUp } from 'lucide-react';
-import { FC, useMemo, useState } from 'react';
+import { FC, useCallback, useMemo, useState } from 'react';
+import { useParams } from 'react-router';
import { RunningStatus } from '../dataset/constant';
import ProcessLogModal, { ILogInfo } from '../process-log-modal';
+import { useIngestionMessages } from '../ingestion-message-hooks';
import { LogTabs } from './dataset-common';
import { DocumentLog, FileLogsTableProps, IFileLogItem } from './interface';
@@ -343,8 +347,19 @@ const FileLogsTable: FC<FileLogsTableProps> = ({
const { t } = useTranslate('knowledgeDetails');
const { t: tDatasetOverview } = useTranslate('datasetOverview');
const [isModalVisible, setIsModalVisible] = useState(false);
- const [logInfo, setLogInfo] = useState<IFileLogItem>();
- const showLog = (row: Row<IFileLogItem & DocumentLog>) => {
+ const [logInfo, setLogInfo] = useState<ILogInfo>();
+ const [selectedLogID, setSelectedLogID] = useState<string>();
+ const { id: routeId } = useParams();
+ const { knowledgeId } = useGetKnowledgeSearchParams();
+ const datasetId = knowledgeId || routeId;
+ const isGoBackend = useIsGoBackend();
+ const {
+ data: messages,
+ fetchPreviousPage,
+ hasPreviousPage,
+ isFetchingPreviousPage,
+ } = useIngestionMessages(datasetId, selectedLogID, isModalVisible);
+ const showLog = useCallback((row: Row<IFileLogItem & DocumentLog>) => {
const logDetail = {
taskId: row.original?.dsl?.task_id,
fileName: row.original.document_name,
@@ -355,18 +370,43 @@ const FileLogsTable: FC<FileLogsTableProps> = ({
duration: formatSecondsToHumanReadable(
row.original.process_duration || 0,
),
- details: row.original.progress_msg,
- } as unknown as IFileLogItem;
- console.log('logDetail', logDetail);
+ details: row.original.progress_msg ?? '',
+ } as ILogInfo;
setLogInfo(logDetail);
+ setSelectedLogID(row.original.id);
setIsModalVisible(true);
- };
+ }, []);
+ const modalLogInfo = useMemo<ILogInfo | undefined>(() => {
+ if (!logInfo) {
+ return undefined;
+ }
+ if (!isGoBackend) {
+ return logInfo;
+ }
+ return {
+ ...logInfo,
+ details: '',
+ events: messages?.items ?? [],
+ loadPreviousEvents: hasPreviousPage
+ ? () => fetchPreviousPage()
+ : undefined,
+ hasPreviousEvents: hasPreviousPage,
+ isLoadingPreviousEvents: isFetchingPreviousPage,
+ };
+ }, [
+ isGoBackend,
+ logInfo,
+ messages,
+ fetchPreviousPage,
+ hasPreviousPage,
+ isFetchingPreviousPage,
+ ]);
const { dataSourceInfo } = useDataSourceInfo();
const columns = useMemo(() => {
return active === LogTabs.FILE_LOGS
? getFileLogsTableColumns(t, showLog, dataSourceInfo)
: getDatasetLogsTableColumns(t, showLog);
- }, [active, t]);
+ }, [active, dataSourceInfo, showLog, t]);
const currentPagination = useMemo(
() => ({
@@ -459,7 +499,7 @@ const FileLogsTable: FC<FileLogsTableProps> = ({
title={active === LogTabs.FILE_LOGS ? t('fileLogs') : t('datasetLog')}
visible={isModalVisible}
onCancel={() => setIsModalVisible(false)}
- logInfo={logInfo as unknown as ILogInfo}
+ logInfo={modalLogInfo as ILogInfo}
/>
)}
</div>
diff --git a/web/src/pages/dataset/dataset/hooks.test.ts b/web/src/pages/dataset/dataset/hooks.test.ts
index 2d2f4a19cb..897b6481af 100644
--- a/web/src/pages/dataset/dataset/hooks.test.ts
+++ b/web/src/pages/dataset/dataset/hooks.test.ts
@@ -40,12 +40,17 @@ jest.mock('@/hooks/use-document-request', () => ({
jest.mock('@/services/knowledge-service', () => ({
listDataPipelineLogDocument: jest.fn(),
+ listIngestionMessages: jest.fn(),
}));
-import { listDataPipelineLogDocument } from '@/services/knowledge-service';
+import {
+ listDataPipelineLogDocument,
+ listIngestionMessages,
+} from '@/services/knowledge-service';
import { useShowLog } from './hooks';
const mockList = jest.mocked(listDataPipelineLogDocument);
+const mockMessages = jest.mocked(listIngestionMessages);
function makeDoc(overrides: Partial<Doc> = {}): Doc {
return {
@@ -92,16 +97,14 @@ describe('useShowLog — Python backend is unaffected by the early-log fallback'
expect(mockList).not.toHaveBeenCalled();
});
- it('leaves an empty progress_msg empty, exactly as before', () => {
+ it('uses a placeholder for an empty progress message', () => {
mockIsGo = false;
const doc = makeDoc({ run: RunningStatus.UNSTART, progress_msg: '' });
const { result } = renderLogs([doc]);
act(() => result.current.showLog(doc));
- // details is the document's own progress_msg, verbatim (the legacy "-"
- // placeholder only applies when there is no source document at all).
- expect(result.current.logInfo.details).toBe(doc.progress_msg);
+ expect(result.current.logInfo.details).toBe('-');
expect(mockList).not.toHaveBeenCalled();
});
@@ -116,14 +119,121 @@ describe('useShowLog — Python backend is unaffected by the early-log fallback'
});
describe('useShowLog — Go backend early-log fallback', () => {
- it('shows the queued message from the early pipeline log row', async () => {
+ it('performs one finishing poll after terminal before stopping', async () => {
mockIsGo = true;
+ jest.useFakeTimers();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
mockList.mockResolvedValue({
+ data: {
+ data: { logs: [{ id: 'run-1', document_id: 'doc-1' }], total: 1 },
+ },
+ } as any);
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ mockMessages.mockResolvedValue({
data: {
data: {
- logs: [{ document_id: 'doc-1', progress_msg: 'Task is queued...' }],
- total: 1,
+ run_count: 1,
+ items: [],
+ has_more_before: false,
+ has_more_after: false,
+ terminal: true,
+ },
+ },
+ } as any);
+
+ try {
+ const { result } = renderLogs([
+ makeDoc({ ingestion_status: IngestionTaskStatus.COMPLETED }),
+ ]);
+ act(() =>
+ result.current.showLog(
+ makeDoc({ ingestion_status: IngestionTaskStatus.COMPLETED }),
+ ),
+ );
+ await waitFor(() => expect(mockMessages).toHaveBeenCalledTimes(1));
+ await act(async () => {
+ jest.advanceTimersByTime(5000);
+ await Promise.resolve();
+ });
+ await waitFor(() => expect(mockMessages).toHaveBeenCalledTimes(2));
+ } finally {
+ jest.useRealTimers();
+ }
+ });
+
+ it('exposes a previous-page loader for historical event scrolling', async () => {
+ mockIsGo = true;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ mockList.mockResolvedValue({
+ data: {
+ data: { logs: [{ id: 'run-1', document_id: 'doc-1' }], total: 1 },
+ },
+ } as any);
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ mockMessages.mockResolvedValue({
+ data: {
+ data: {
+ run_count: 1,
+ items: [
+ {
+ id: 20,
+ ts: '2026-01-01T00:00:00Z',
+ event_type: 1,
+ component: '',
+ phase: 0,
+ message: 'latest',
+ },
+ ],
+ oldest_id: 20,
+ newest_id: 20,
+ has_more_before: true,
+ has_more_after: false,
+ terminal: false,
+ },
+ },
+ } as any);
+
+ const { result } = renderLogs([
+ makeDoc({ ingestion_status: IngestionTaskStatus.RUNNING }),
+ ]);
+ act(() =>
+ result.current.showLog(
+ makeDoc({ ingestion_status: IngestionTaskStatus.RUNNING }),
+ ),
+ );
+ await waitFor(() => expect(mockMessages).toHaveBeenCalledTimes(1));
+ expect(result.current.logInfo.loadPreviousEvents).toEqual(
+ expect.any(Function),
+ );
+ expect(result.current.logInfo.hasPreviousEvents).toBe(true);
+ });
+
+ it('loads messages by the exact pipeline log identity', async () => {
+ mockIsGo = true;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ mockList.mockResolvedValue({
+ data: {
+ data: { logs: [{ id: 'run-2', document_id: 'doc-1' }], total: 1 },
+ },
+ } as any);
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ mockMessages.mockResolvedValue({
+ data: {
+ data: {
+ run_count: 2,
+ items: [
+ {
+ id: 12,
+ ts: '2026-01-01T00:00:00Z',
+ event_type: 1,
+ component: '',
+ phase: 0,
+ message: 'Task is queued...',
+ },
+ ],
+ has_more_before: false,
+ has_more_after: false,
+ terminal: false,
},
},
} as any);
@@ -133,8 +243,45 @@ describe('useShowLog — Go backend early-log fallback', () => {
act(() => result.current.showLog(doc));
await waitFor(() =>
- expect(result.current.logInfo.details).toBe('Task is queued...'),
+ expect(
+ result.current.logInfo.events?.map(
+ (event: { message: string }) => event.message,
+ ),
+ ).toEqual(['Task is queued...']),
+ );
+ expect(mockList).toHaveBeenCalledWith(
+ 'kb-1',
+ expect.objectContaining({
+ document_id: 'doc-1',
+ log_type: 'file',
+ orderby: 'run_count',
+ desc: true,
+ page_size: 1,
+ }),
);
+ expect(mockMessages).toHaveBeenCalledWith('kb-1', 'run-2', {
+ limit: 200,
+ });
+ });
+
+ it('does not read the old pipeline log progress field on Go', async () => {
+ mockIsGo = true;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ mockList.mockResolvedValue({
+ data: {
+ data: {
+ logs: [{ document_id: 'doc-1', progress_msg: 'Task is queued...' }],
+ total: 1,
+ },
+ },
+ } as any);
+
+ const doc = makeDoc({ ingestion_status: IngestionTaskStatus.CREATED });
+ const { result } = renderLogs([doc]);
+ act(() => result.current.showLog(doc));
+
+ await waitFor(() => expect(mockList).toHaveBeenCalled());
+ expect(result.current.logInfo.details).toBe('-');
expect(result.current.logInfo.status).toBe(RunningStatus.QUEUED);
expect(mockList).toHaveBeenCalledWith(
'kb-1',
@@ -164,11 +311,11 @@ describe('useShowLog — Go backend early-log fallback', () => {
// The name search would return other documents' rows and could push this
// document's queued row past the first page.
expect(mockList.mock.calls[0][1]).not.toHaveProperty('keywords');
- // No row came back, so the document's own (empty) progress_msg stands.
- expect(result.current.logInfo.details).toBe('');
+ // No run row means there is no current event to display.
+ expect(result.current.logInfo.details).toBe('-');
});
- it('does not query once the document is running', () => {
+ it('uses the current log identity even when the document is running', async () => {
mockIsGo = true;
const doc = makeDoc({
ingestion_status: IngestionTaskStatus.RUNNING,
@@ -179,7 +326,7 @@ describe('useShowLog — Go backend early-log fallback', () => {
const { result } = renderLogs([doc]);
act(() => result.current.showLog(doc));
- expect(result.current.logInfo.details).toBe('Indexing done');
- expect(mockList).not.toHaveBeenCalled();
+ await waitFor(() => expect(mockList).toHaveBeenCalled());
+ expect(result.current.logInfo.details).toBe('-');
});
});
diff --git a/web/src/pages/dataset/dataset/hooks.ts b/web/src/pages/dataset/dataset/hooks.ts
index 88ac71959c..598f6b6603 100644
--- a/web/src/pages/dataset/dataset/hooks.ts
+++ b/web/src/pages/dataset/dataset/hooks.ts
@@ -10,18 +10,15 @@ import { useCallback, useMemo, useState } from 'react';
import { useParams } from 'react-router';
import { listDataPipelineLogDocument } from '@/services/knowledge-service';
import { ILogInfo } from '../process-log-modal';
-import { RunningStatus } from './constant';
-import {
- getDocumentRunningStatus,
- ingestionStatusToRunningStatus,
-} from './utils';
+import { getDocumentProgressMessage, getDocumentRunningStatus } from './utils';
import type { IFileLogList } from '../dataset-overview/interface';
+import { useIngestionMessages } from '../ingestion-message-hooks';
const PollIntervalMs = 5000;
export const DocumentLogKeys = {
queued: (datasetId: string | undefined, documentId: string | undefined) =>
- ['queuedDocumentLog', datasetId, documentId] as const,
+ ['documentLog', datasetId, documentId] as const,
};
export const useShowLog = (documents: IDocumentInfo[]) => {
@@ -45,54 +42,57 @@ export const useShowLog = (documents: IDocumentInfo[]) => {
liveDoc ??
documents.find((item: IDocumentInfo) => item.id === record?.id) ??
record;
- const queued =
- isGoBackend &&
- !!sourceDoc &&
- ingestionStatusToRunningStatus(sourceDoc.ingestion_status) ===
- RunningStatus.QUEUED;
-
- // The Go backend reports a queued document via ingestion_status while the
- // legacy document.progress_msg stays empty until the worker starts. Fall
- // back to the early pipeline-operation-log row the Go API writes at task
- // creation, so the queued modal shows "Task is queued..." instead of "-".
- // Python never sets ingestion_status, so this query stays disabled there.
- const { data: queuedLog } = useQuery<IFileLogList>({
+ // A document can have several historical runs. The list endpoint's exact
+ // document filter returns the current log identity, which scopes all event
+ // reads to the intended run.
+ const { data: documentLog } = useQuery<IFileLogList>({
queryKey: DocumentLogKeys.queued(datasetId, sourceDoc?.id),
- enabled: visible && queued && !!datasetId && !!sourceDoc?.id,
+ enabled: visible && isGoBackend && !!datasetId && !!sourceDoc?.id,
refetchInterval: PollIntervalMs,
queryFn: async () => {
const { data: res = {} } = await listDataPipelineLogDocument(
datasetId || '',
{
page: 1,
- page_size: 10,
// Exact match on the document: a name search is fuzzy and can push
// this document's row off the first page when several documents
- // share a name, silently losing the queued message.
+ // share a name.
document_id: sourceDoc?.id,
log_type: 'file',
+ orderby: 'run_count',
+ desc: true,
+ page_size: 1,
},
);
return (res.data || { logs: [], total: 0 }) as IFileLogList;
},
});
- const queuedProgressMsg = useMemo(() => {
- const logs = queuedLog?.logs ?? [];
- // The endpoint filters by document_id, so the first row is this document's
- // newest log. A miss falls back to the document progress_msg below.
- return logs[0]?.progress_msg;
- }, [queuedLog]);
- // The queued fallback is only meaningful while the document is still
- // queued. Once it starts running, the query cache may still hold the stale
- // "Task is queued..." message and would shadow the live progress below.
- const effectiveQueuedProgressMsg = queued ? queuedProgressMsg : undefined;
+ const logID = documentLog?.logs[0]?.id;
+ const {
+ data: messages,
+ fetchPreviousPage,
+ hasPreviousPage,
+ isFetchingPreviousPage,
+ } = useIngestionMessages(datasetId, logID, visible);
+ const latestEvent = useMemo(() => {
+ const items = messages?.items ?? [];
+ return items[items.length - 1];
+ }, [messages]);
const logInfo = useMemo(() => {
const source = sourceDoc;
let log: ILogInfo = {
taskId: source?.id,
fileName: source?.name || '-',
- details: effectiveQueuedProgressMsg || source?.progress_msg || '-',
+ details: source
+ ? messages?.items.length
+ ? ''
+ : getDocumentProgressMessage({
+ ...source,
+ latest_ingestion_event:
+ latestEvent ?? source.latest_ingestion_event,
+ })
+ : '-',
};
if (source) {
log = {
@@ -107,11 +107,30 @@ export const useShowLog = (documents: IDocumentInfo[]) => {
// Go derives status from ingestion_status (queued included);
// Python reads the legacy run field.
status: getDocumentRunningStatus(source),
- details: effectiveQueuedProgressMsg || source.progress_msg,
+ details: messages?.items.length
+ ? ''
+ : getDocumentProgressMessage({
+ ...source,
+ latest_ingestion_event:
+ latestEvent ?? source.latest_ingestion_event,
+ }),
+ events: messages?.items,
+ loadPreviousEvents: hasPreviousPage
+ ? () => fetchPreviousPage()
+ : undefined,
+ hasPreviousEvents: hasPreviousPage,
+ isLoadingPreviousEvents: isFetchingPreviousPage,
};
}
return log;
- }, [sourceDoc, effectiveQueuedProgressMsg]);
+ }, [
+ sourceDoc,
+ latestEvent,
+ messages,
+ fetchPreviousPage,
+ hasPreviousPage,
+ isFetchingPreviousPage,
+ ]);
const showLog = useCallback(
(data: IDocumentInfo) => {
setRecord(data);
diff --git a/web/src/pages/dataset/dataset/parsing-card.tsx b/web/src/pages/dataset/dataset/parsing-card.tsx
index b0902e62c4..17d6d6f13f 100644
--- a/web/src/pages/dataset/dataset/parsing-card.tsx
+++ b/web/src/pages/dataset/dataset/parsing-card.tsx
@@ -3,7 +3,7 @@ import { IDocumentInfo } from '@/interfaces/database/document';
import { useTranslation } from 'react-i18next';
import reactStringReplace from 'react-string-replace';
import { RunningStatus, RunningStatusMap } from './constant';
-import { getDocumentRunningStatus } from './utils';
+import { getDocumentProgressMessage, getDocumentRunningStatus } from './utils';
interface IProps {
record: IDocumentInfo;
@@ -59,7 +59,7 @@ export const PopoverContent = ({ record }: IProps) => {
{
key: 'progress_msg',
label: t('knowledgeDetails.progressMsg'),
- children: replaceText((record.progress_msg || '').trim()),
+ children: replaceText(getDocumentProgressMessage(record).trim()),
},
];
diff --git a/web/src/pages/dataset/dataset/utils.test.ts b/web/src/pages/dataset/dataset/utils.test.ts
index bc903f8e1a..2ff42a15b9 100644
--- a/web/src/pages/dataset/dataset/utils.test.ts
+++ b/web/src/pages/dataset/dataset/utils.test.ts
@@ -10,6 +10,7 @@ import {
findFilesParserGaps,
findParserGap,
getDocumentRunningStatus,
+ getDocumentProgressMessage,
getFileTypeByExtension,
getSavedParserSetups,
hasUnsupportedTypeGap,
@@ -38,6 +39,54 @@ describe('isDocumentStopping', () => {
});
});
+describe('getDocumentProgressMessage', () => {
+ afterEach(() => {
+ mockIsGoBackend = false;
+ });
+
+ it('reads the latest real ingestion event on Go', () => {
+ mockIsGoBackend = true;
+
+ expect(
+ getDocumentProgressMessage({
+ progress_msg: 'stale legacy text',
+ latest_ingestion_event: {
+ id: 42,
+ ts: '2026-01-01T00:00:00Z',
+ event_type: 1,
+ component: '',
+ phase: 0,
+ message: 'Indexing 4/10',
+ },
+ }),
+ ).toBe('Indexing 4/10');
+ });
+
+ it('keeps the Python progress field unchanged', () => {
+ mockIsGoBackend = false;
+
+ expect(
+ getDocumentProgressMessage({
+ progress_msg: 'Parsing chunks',
+ latest_ingestion_event: {
+ id: 42,
+ ts: '2026-01-01T00:00:00Z',
+ event_type: 1,
+ component: '',
+ phase: 0,
+ message: 'Go-only event',
+ },
+ }),
+ ).toBe('Parsing chunks');
+ });
+
+ it('returns a placeholder when the selected source has no message', () => {
+ mockIsGoBackend = true;
+
+ expect(getDocumentProgressMessage({ progress_msg: '' })).toBe('-');
+ });
+});
+
describe('isDocumentProcessing', () => {
afterEach(() => {
mockIsGoBackend = false;
diff --git a/web/src/pages/dataset/dataset/utils.ts b/web/src/pages/dataset/dataset/utils.ts
index 220115e93d..ad002cd73d 100644
--- a/web/src/pages/dataset/dataset/utils.ts
+++ b/web/src/pages/dataset/dataset/utils.ts
@@ -96,6 +96,15 @@ export const getDocumentRunningStatus = (
python: document.run ?? RunningStatus.UNSTART,
});
+/** Returns the backend-specific message shown for a document's current run. */
+export const getDocumentProgressMessage = (
+ document: Pick<IDocumentInfo, 'progress_msg' | 'latest_ingestion_event'>,
+) =>
+ pickByBackend({
+ go: document.latest_ingestion_event?.message,
+ python: document.progress_msg,
+ }) || '-';
+
/**
* Whether a cancel request is currently in flight for the document.
* Only the Go backend reports STOPPING; Python always returns false.
@@ -188,11 +197,14 @@ export const hasUnsupportedTypeGap = (gaps: ParserGap[]) =>
// Modal copy keyed by gap kind: missing models steer to adding the model,
// unsupported types to reselecting the parse method.
-export const pickByGapKind = <T>(
+export function pickByGapKind<T>(
gaps: ParserGap[],
options: { missingModel: T; unsupportedType: T },
-): T =>
- hasUnsupportedTypeGap(gaps) ? options.unsupportedType : options.missingModel;
+): T {
+ return hasUnsupportedTypeGap(gaps)
+ ? options.unsupportedType
+ : options.missingModel;
+}
type ParserSetup = Record<string, any> & { fileFormat?: string };
diff --git a/web/src/pages/dataset/ingestion-message-hooks.ts b/web/src/pages/dataset/ingestion-message-hooks.ts
new file mode 100644
index 0000000000..f325b9a20b
--- /dev/null
+++ b/web/src/pages/dataset/ingestion-message-hooks.ts
@@ -0,0 +1,222 @@
+import {
+ IngestionMessagesResponse,
+ IngestionMessageParams,
+} from '@/interfaces/database/ingestion';
+import { listIngestionMessages } from '@/services/knowledge-service';
+import { useIsGoBackend } from '@/utils/backend-variant';
+import { useInfiniteQuery, useQueryClient } from '@tanstack/react-query';
+import type { InfiniteData } from '@tanstack/react-query';
+import { useEffect, useRef, useState } from 'react';
+
+const PollIntervalMs = 5000;
+
+export const IngestionMessageKeys = {
+ messages: (datasetId: string | undefined, logId: string | undefined) =>
+ ['ingestionMessages', datasetId, logId] as const,
+};
+
+export const useIngestionMessages = (
+ datasetId: string | undefined,
+ logId: string | undefined,
+ enabled: boolean,
+ params: IngestionMessageParams = { limit: 200 },
+) => {
+ const queryClient = useQueryClient();
+ const isGoBackend = useIsGoBackend();
+ const [finishing, setFinishing] = useState(false);
+ const finishingRef = useRef(false);
+
+ const query = useInfiniteQuery<
+ IngestionMessagesResponse,
+ Error,
+ InfiniteData<IngestionMessagesResponse, IngestionMessageParams | undefined>,
+ readonly unknown[],
+ IngestionMessageParams | undefined
+ >({
+ queryKey: IngestionMessageKeys.messages(datasetId, logId),
+ enabled: enabled && isGoBackend && !!datasetId && !!logId,
+ initialPageParam: undefined,
+ queryFn: async ({ pageParam }) => {
+ const { data: res = {} } = await listIngestionMessages(
+ datasetId || '',
+ logId || '',
+ { ...params, ...pageParam },
+ );
+ return res.data as IngestionMessagesResponse;
+ },
+ getNextPageParam: (lastPage) =>
+ lastPage.has_more_after && lastPage.newest_id
+ ? { after_id: lastPage.newest_id }
+ : undefined,
+ getPreviousPageParam: (firstPage) =>
+ firstPage.has_more_before && firstPage.oldest_id
+ ? { before_id: firstPage.oldest_id }
+ : undefined,
+ });
+
+ const { fetchNextPage, hasNextPage, isFetchingNextPage } = query;
+ useEffect(() => {
+ if (hasNextPage && !isFetchingNextPage) {
+ void fetchNextPage();
+ }
+ }, [fetchNextPage, hasNextPage, isFetchingNextPage]);
+
+ const pages = query.data?.pages ?? [];
+ const terminal = pages.some((page) => page.terminal);
+
+ const items = Array.from(
+ new Map(
+ pages.flatMap((page) => page.items).map((item) => [item.id, item]),
+ ).values(),
+ ).sort((left, right) => left.id - right.id);
+
+ const newestId = items.length > 0 ? items[items.length - 1].id : undefined;
+ const newestIdRef = useRef(newestId);
+ newestIdRef.current = newestId;
+
+ useEffect(() => {
+ if (
+ !enabled ||
+ !isGoBackend ||
+ !datasetId ||
+ !logId ||
+ terminal ||
+ finishing
+ ) {
+ return;
+ }
+ const interval = setInterval(async () => {
+ if (isFetchingNextPage || hasNextPage) {
+ return;
+ }
+ try {
+ const afterId = newestIdRef.current;
+ const { data: res = {} } = await listIngestionMessages(
+ datasetId,
+ logId,
+ {
+ ...params,
+ ...(afterId ? { after_id: afterId } : {}),
+ },
+ );
+ const nextResponse = res.data as IngestionMessagesResponse | undefined;
+ if (!nextResponse) {
+ return;
+ }
+ if (nextResponse.items?.length > 0) {
+ queryClient.setQueryData<
+ InfiniteData<
+ IngestionMessagesResponse,
+ IngestionMessageParams | undefined
+ >
+ >(IngestionMessageKeys.messages(datasetId, logId), (oldData) => {
+ if (!oldData) return oldData;
+ return {
+ ...oldData,
+ pages: [...oldData.pages, nextResponse],
+ pageParams: [
+ ...oldData.pageParams,
+ afterId ? { after_id: afterId } : undefined,
+ ],
+ };
+ });
+ } else if (nextResponse.terminal) {
+ queryClient.setQueryData<
+ InfiniteData<
+ IngestionMessagesResponse,
+ IngestionMessageParams | undefined
+ >
+ >(IngestionMessageKeys.messages(datasetId, logId), (oldData) => {
+ if (!oldData || oldData.pages.length === 0) return oldData;
+ const lastIndex = oldData.pages.length - 1;
+ const lastPage = oldData.pages[lastIndex];
+ if (lastPage.terminal) return oldData;
+ const nextPages = [...oldData.pages];
+ nextPages[lastIndex] = { ...lastPage, terminal: true };
+ return { ...oldData, pages: nextPages };
+ });
+ }
+ } catch {
+ // ignore polling error
+ }
+ }, PollIntervalMs);
+ return () => clearInterval(interval);
+ }, [
+ enabled,
+ isGoBackend,
+ datasetId,
+ logId,
+ terminal,
+ finishing,
+ hasNextPage,
+ isFetchingNextPage,
+ params,
+ queryClient,
+ ]);
+
+ useEffect(() => {
+ finishingRef.current = false;
+ setFinishing(false);
+ }, [datasetId, logId]);
+
+ useEffect(() => {
+ if (
+ !terminal ||
+ finishingRef.current ||
+ !enabled ||
+ !isGoBackend ||
+ !datasetId ||
+ !logId
+ ) {
+ return;
+ }
+ finishingRef.current = true;
+ setFinishing(true);
+ const timer = setTimeout(async () => {
+ try {
+ const afterId = newestIdRef.current;
+ const { data: res = {} } = await listIngestionMessages(
+ datasetId,
+ logId,
+ {
+ ...params,
+ ...(afterId ? { after_id: afterId } : {}),
+ },
+ );
+ const nextResponse = res.data as IngestionMessagesResponse | undefined;
+ if (nextResponse?.items?.length) {
+ queryClient.setQueryData<
+ InfiniteData<
+ IngestionMessagesResponse,
+ IngestionMessageParams | undefined
+ >
+ >(IngestionMessageKeys.messages(datasetId, logId), (oldData) => {
+ if (!oldData) return oldData;
+ return {
+ ...oldData,
+ pages: [...oldData.pages, nextResponse],
+ pageParams: [
+ ...oldData.pageParams,
+ afterId ? { after_id: afterId } : undefined,
+ ],
+ };
+ });
+ }
+ } finally {
+ setFinishing(false);
+ }
+ }, PollIntervalMs);
+ return () => clearTimeout(timer);
+ }, [datasetId, enabled, isGoBackend, logId, params, queryClient, terminal]);
+
+ return {
+ ...query,
+ data: pages.length
+ ? {
+ ...pages[0],
+ items,
+ terminal,
+ }
+ : undefined,
+ };
+};
diff --git a/web/src/pages/dataset/process-log-modal.tsx b/web/src/pages/dataset/process-log-modal.tsx
index 03282b4d35..bebc354412 100644
--- a/web/src/pages/dataset/process-log-modal.tsx
+++ b/web/src/pages/dataset/process-log-modal.tsx
@@ -8,10 +8,11 @@ import {
} from '@/components/ui/tooltip';
import { RunningStatusMap } from '@/constants/knowledge';
import { useTranslate } from '@/hooks/common-hooks';
-import React, { useMemo } from 'react';
+import React, { useEffect, useMemo, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import reactStringReplace from 'react-string-replace';
import { RunningStatus } from './dataset/constant';
+import { IngestionEventItem } from '@/interfaces/database/ingestion';
export interface ILogInfo {
fileType?: string;
uploadedBy?: string;
@@ -29,6 +30,10 @@ export interface ILogInfo {
endTime?: string;
duration?: string;
details: string;
+ events?: IngestionEventItem[];
+ loadPreviousEvents?: () => Promise<unknown>;
+ hasPreviousEvents?: boolean;
+ isLoadingPreviousEvents?: boolean;
}
interface ProcessLogModalProps {
@@ -91,10 +96,38 @@ const ProcessLogModal: React.FC<ProcessLogModalProps> = ({
}) => {
const { t } = useTranslate(translateKey || 'knowledgeDetails');
const { t: tc } = useTranslation();
- const blackKeyList = [''];
+ const blackKeyList = [
+ 'loadPreviousEvents',
+ 'hasPreviousEvents',
+ 'isLoadingPreviousEvents',
+ ];
const logInfo = useMemo(() => {
return initData;
}, [initData]);
+ const eventListRef = useRef<HTMLDivElement>(null);
+ const prependHeightRef = useRef<number | null>(null);
+ const eventCount = logInfo.events?.length ?? 0;
+ useEffect(() => {
+ const list = eventListRef.current;
+ const previousHeight = prependHeightRef.current;
+ if (list && previousHeight !== null) {
+ list.scrollTop = list.scrollHeight - previousHeight;
+ }
+ prependHeightRef.current = null;
+ }, [eventCount]);
+
+ const handleEventScroll = (event: React.UIEvent<HTMLDivElement>) => {
+ if (
+ event.currentTarget.scrollTop !== 0 ||
+ !logInfo.hasPreviousEvents ||
+ logInfo.isLoadingPreviousEvents ||
+ !logInfo.loadPreviousEvents
+ ) {
+ return;
+ }
+ prependHeightRef.current = event.currentTarget.scrollHeight;
+ void logInfo.loadPreviousEvents();
+ };
return (
<Modal
@@ -117,6 +150,40 @@ const ProcessLogModal: React.FC<ProcessLogModalProps> = ({
) {
return null;
}
+ if (key === 'events') {
+ return (
+ <div className="w-full mt-2" key={key}>
+ <InfoItem
+ label={t('details')}
+ value={
+ <div
+ ref={eventListRef}
+ onScroll={handleEventScroll}
+ className="w-full whitespace-pre-line text-wrap bg-bg-card rounded-lg h-fit max-h-[350px] overflow-y-auto scrollbar-auto p-2.5"
+ >
+ {logInfo.isLoadingPreviousEvents && (
+ <div className="text-text-secondary text-xs mb-1">
+ Loading earlier events…
+ </div>
+ )}
+ {logInfo.events?.map((event) => (
+ <div
+ className={
+ event.event_type === 3
+ ? 'text-text-secondary'
+ : undefined
+ }
+ key={event.id}
+ >
+ {replaceText(event.message)}
+ </div>
+ ))}
+ </div>
+ }
+ />
+ </div>
+ );
+ }
if (key === 'details') {
return (
<div className="w-full mt-2" key={key}>
@@ -151,7 +218,7 @@ const ProcessLogModal: React.FC<ProcessLogModalProps> = ({
<InfoItem
overflowTip={true}
label={t(key)}
- value={logInfo[key as keyof typeof logInfo]}
+ value={logInfo[key as keyof typeof logInfo] as string}
/>
</div>
);
diff --git a/web/src/services/knowledge-service.ts b/web/src/services/knowledge-service.ts
index 0739c11d90..06ceb7f7a8 100644
--- a/web/src/services/knowledge-service.ts
+++ b/web/src/services/knowledge-service.ts
@@ -509,6 +509,12 @@ export const listPipelineDatasetLogs = (
export const getPipelineDetail = (datasetId: string, logId: string) =>
request.get(api.getPipelineDetail(datasetId, logId));
+export const listIngestionMessages = (
+ datasetId: string,
+ logId: string,
+ params?: Record<string, any>,
+) => request.get(api.listIngestionMessages(datasetId, logId), { params });
+
export const getKnowledgeBasicInfo = (datasetId: string) =>
request.get(api.getKnowledgeBasicInfo(datasetId));
diff --git a/web/src/utils/api.ts b/web/src/utils/api.ts
index d2c074cc7d..62e0975f66 100644
--- a/web/src/utils/api.ts
+++ b/web/src/utils/api.ts
@@ -224,6 +224,8 @@ export default {
`${restAPIv1}/datasets/${datasetId}/ingestions`,
getPipelineDetail: (datasetId: string, logId: string) =>
`${restAPIv1}/datasets/${datasetId}/ingestions/${logId}`,
+ listIngestionMessages: (datasetId: string, logId: string) =>
+ `${restAPIv1}/datasets/${datasetId}/ingestions/${logId}/messages`,
fetchPipelineDatasetLogs: (datasetId: string) =>
`${restAPIv1}/datasets/${datasetId}/ingestions`,
listPipelines: `${restAPIv1}/pipelines?type=builtin`,