Skip to content

fix(ingestion): settle a terminal task's open run before deletion - #21798

Merged
yuzhichang merged 1 commit into
infiniflow:mainfrom
ccclucky:fix/remove-settles-terminal-run
Nov 2, 2026
Merged

yuzhichang merged 1 commit into
infiniflow:mainfrom
ccclucky:fix/remove-settles-terminal-run

Conversation

@ccclucky

Copy link
Copy Markdown
Contributor

Summary

IngestionTaskService.Remove settles a task's numbered run ledger before deleting the row, but only for queued (CREATED/SCHEDULED) and STOPPED tasks. COMPLETED/FAILED tasks are deleted without that step.

The worker closes the ledger before flipping the task to a terminal state, and that close is best-effort: recordTerminalPipelineLog only logs a warning when RecordPipelineLog/RecordTerminal fail (internal/ingestion/service/ingestion_service.go), then MarkCompleted/MarkFailed still runs. A transient DB error therefore leaves a COMPLETED/FAILED task whose run is still in an open operation_status. Deleting the task removes the only reference that could settle that run — no worker remains — so the run shows as running in the parse history forever.

PrepareDocumentTaskForCleanup already settles the ledger for COMPLETED/FAILED tasks for the same reason; Remove is the missing sibling.

Change

  • Map each terminal task status to the run settlement it owns (STOPPED → CANCEL, COMPLETED → DONE, FAILED → FAIL) and settle before deleting the task row.
  • Settlement is idempotent: AdvanceOpenLog only updates open states and ensureTerminalEvent skips when a terminal event already exists, so an already-closed run is a no-op.

Test plan

  • TestIngestionTaskServiceRemoveSettlesTerminalTaskRun seeds a FAILED and a COMPLETED task bound to an open numbered run, calls Remove, and asserts the run reaches FAIL/DONE with the matching terminal event. Red on the base commit (numbered run status = "0"), green with the change.
  • go test -tags cgo,static ./internal/service/ passes.

The worker closes the run ledger best-effort before flipping the task to
COMPLETED/FAILED, so a lost terminal write leaves a run open with no worker
left to settle it. Remove only repaired this for queued/STOPPED tasks, so
deleting such a terminal task orphaned the ledger and the parse history kept
showing the run as running. Settle COMPLETED/FAILED runs the same way before
deleting the task row, matching PrepareDocumentTaskForCleanup.
@ccclucky

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 08153187-9e9a-4fd7-bf40-baae9ea1f517
Commits

Reviewing files that changed from the base of the PR and between 94369fc and 9e5c3fa.

Files selected for processing: 2
  • internal/service/ingestion_task_service.go
  • internal/service/ingestion_task_service_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

Task removal now settles numbered pipeline runs for STOPPED, COMPLETED, and FAILED tasks before deletion. It uses a status-specific ledger outcome and message. Tests cover settlement for COMPLETED and FAILED tasks.

Changes: Terminal run settlement

Layer / File(s) Summary
Map and settle terminal task runs
internal/service/ingestion_task_service.go, internal/service/ingestion_task_service_test.go
Remove maps STOPPED, COMPLETED, and FAILED tasks to their corresponding run-ledger status and message. It settles a run only when a pipeline-log ID is present and the run exists with a positive count. Missing runs are ignored; other lookup or settlement errors prevent deletion. Tests verify settlement for COMPLETED and FAILED tasks.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

Suggested labels: 🐞 bug, go, parsing, size:S

Suggested reviewers: jay77721

Merge Risk: Minimal · up to 9e5c3

No merge-blocking issue is established from the supplied evidence.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title names the main change: Remove now settles a terminal task's open run before deleting the task. It follows the fix(ingestion) convention and matches the raw summary, PR description, and objec…
Description check Passed The description covers the problem, the cause, the change, idempotency, and a test plan. It is detailed and matches the diff and the objectives. It does not use the repository template's heading exact…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: Remove now settles a terminal task's open run before deleting the task. It follows the fix(ingestion) convention and matches the raw summary, PR description, and objectives. It is specific and free of noise.

Full details: Description check

Explanation

The description covers the problem, the cause, the change, idempotency, and a test plan. It is detailed and matches the diff and the objectives. It does not use the repository template's heading exactly. It uses a '## Summary' heading, but the required '### Summary' section is present in substance, and the content explains purpose and background for reviewers.

Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks the ledger at dawn.
Three terminal paths are neatly drawn.
The run receives its ending note.
Then task removal sets afloat.
Soft paws hop on, the work is done.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor
⚠️ Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator

Discussion: I verified the terminalRunSettlement mapping on a verbatim extraction: STOPPED, COMPLETED and FAILED settle to cancel, done and fail respectively, and non-terminal statuses (RUNNING, PENDING, empty) do not settle. The old branch in Remove only settled STOPPED, so a completed or failed task whose terminal write was lost kept an open ledger with no worker left to close it. That Remove now settles those cases end to end, including the idempotency and the no-commit-row-on-reject behavior, is covered by TestIngestionTaskServiceRemoveSettlesTerminalTaskRun, which I could not run on my host (package dependency tree does not build in my sandbox), so that part is read-only for me. Nothing blocking from what I can verify.

@ccclucky

Copy link
Copy Markdown
Contributor Author

{"body": "Thanks for the read-only verification \u2014 the mapping reading matches what the code does.\n\nI can cover the part you could not run. On head 9e5c3fa62, this host:\n\n\nCGO_ENABLED=1 go test -tags cgo,static -count=1 -run TestIngestionTaskServiceRemoveSettlesTerminalTaskRun -v ./internal/service/\n=== RUN TestIngestionTaskServiceRemoveSettlesTerminalTaskRun\n=== RUN TestIngestionTaskServiceRemoveSettlesTerminalTaskRun/failed\n=== RUN TestIngestionTaskServiceRemoveSettlesTerminalTaskRun/completed\n--- PASS: TestIngestionTaskServiceRemoveSettlesTerminalTaskRun (0.02s)\nPASS\nok ragflow/internal/service 1.422s\n\n\nTwo notes on reproducing it:\n\n- internal/service pulls agentic_rag/decisionhead, which only compiles under the cgo tag, so a plain ./internal/service/ build fails with undefined: OpenSession \u2014 the cgo,static tags are required.\n- Linking the test binary here needs the ORT relaxation: plain libonnxruntime.a instead of -Wl,-force_load, plus -Wl,-u,_OrtGetApiBase -framework Foundation; otherwise Apple ld stops on _NSLog / OrtInteropAPI::*.\n\nBoth subtests (completed, failed) pass, so the terminal-settlement path is exercised for both statuses on this head. Nothing pending from my side."}

@xugangqiang

Copy link
Copy Markdown
Contributor

LGTM

@yuzhichang yuzhichang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed merge-base(main)..9e5c3fa — internal/service/ingestion_task_service.go, internal/service/ingestion_task_service_test.go.

What the diff does

Remove previously settled only a STOPPED task's open run ledger. The new terminalRunSettlement helper centralizes the terminal-status to run-status mapping (STOPPED→CANCEL, COMPLETED→DONE, FAILED→FAIL) and is applied to every terminal task before the row is deleted, so a lost best-effort terminal write no longer leaves a pipeline_operation_log row stuck in an open status with no worker left to close it. The RunCount > 0 gate, gorm.ErrRecordNotFound tolerance, and the idempotent settleTaskRun/ensureTerminalEvent path are unchanged.

Verification

  • STOPPED mapping is identical to the base branch, so there is no behavior change for the previously covered case.
  • COMPLETED/FAILED reuse the same taskTerminalMessageComplete / taskTerminalMessageFailed constants and TaskStatusDone / TaskStatusFail values that MarkCompleted/MarkFailed and PrepareDocumentTaskForCleanup already use, so no second source of truth or dual path is introduced.
  • settleTaskRun remains idempotent: advanceOpenLogIfOpen only moves rows in OpenPipelineOperationStatuses(), and ensureTerminalEvent skips insertion when a terminal event already exists — so retrying after a failed settlement, or racing a late worker write, is safe.
  • Missing/deleted ledger rows are still tolerated for terminal tasks (ErrRecordNotFound ignored), so the fix does not make a task undeletable when its run row is gone.
  • New test covers both FAILED and COMPLETED with an open numbered run and asserts the run status plus the terminal event message.
  • No enterprise surface, build tags, or GORM Take with a bare string ID is touched. The new test is untagged and uses the existing in-memory SQLite service harness, so it stays in the unit tier.

No blocking findings.

@yuzhichang
yuzhichang merged commit e7d7728 into infiniflow:main Nov 2, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants