Skip to content

fix(chunker): assemble the One chunk in reading order - #22940

Open
1BugKiller wants to merge 1 commit into
infiniflow:mainfrom
1BugKiller:fix/one-chunk-reading-order
Open

1BugKiller wants to merge 1 commit into
infiniflow:mainfrom
1BugKiller:fix/one-chunk-reading-order

Conversation

@1BugKiller

Copy link
Copy Markdown
Contributor

Fixes #22939

What was wrong

The One parser claims "one file forms a chunk which maintains original text order", but the Go port joined the parsed blocks in upstream order. A PDF whose blocks arrive out of order (multi-column layouts, layout models that emit per-region results) produced a single chunk with swapped text:

item order:  SECOND page-1 block, FIRST page-1 block, page-2 block
before:      "SECOND block on page one.\nFIRST block on page one.\nBlock on page two."

The General parser on the same input already emits the sorted order, so the two parsers disagreed on the same document. Python's rag/app/one.py sorts its sections by (page, top, left) before joining.

The change

Sort the items with the shared sortPDFUnits before joining, under the same condition the position merge already uses — a PDF candidate that is not a spreadsheet:

mergePositions := pdfCandidate && !hasOneChunkSpreadsheetItems(src)
if mergePositions {
    src = sortPDFUnits(src)
}

sortPDFUnits only reorders contiguous runs of positioned units; units without coordinates and units that declare _preserve_order are barriers that keep their upstream position. Non-PDF payloads and spreadsheet items therefore behave exactly as before.

Tests

internal/ingestion/component/chunker/one_test.go:

  • TestOneChunker_KeepsReadingOrderForOutOfOrderPDFItems — three blocks given out of order. Before: "SECOND …\nFIRST …\nBlock on page two.". After: "FIRST …\nSECOND …\nBlock on page two.".

The existing One tests still pass, including TestOneChunker_MultiItemMergesPDFPositions (position merge unchanged), TestOneChunker_JSONMultipleMerges (position-less items keep their order) and TestOneChunker_MultiItemSpreadsheetSkipsPDFPositions (spreadsheets are not sorted).

Verification

go test ./internal/ingestion/component/chunker/ -run TestOneChunker   → ok
go test ./internal/ingestion/component/chunker/ -count=1
--- FAIL: TestChunkerGoldenParity

TestChunkerGoldenParity fails on this base as well: it compares snapshots byte-for-byte and git checks them out with CRLF on Windows. Verified pre-existing on main.

gofmt, go vet and the repo's tools/hooks/check_files.py checks (eof, trailing whitespace, mixed line endings, ASCII comments, case conflicts, merge conflicts) are clean.

Python's one.py sorts its sections by (page, top, left) before joining them, and
the general chunker applies the same sort through sortPDFUnits. The One chunker
joined the JSON items in upstream order, so a PDF whose blocks arrive out of
order — multi-column layouts, layout models that emit per-region results —
produced a single chunk with scrambled text.

Sort the items with the shared sortPDFUnits before joining, under the same
condition the position merge already uses: a PDF candidate that is not a
spreadsheet. Units without coordinates and units that declare PreserveOrder stay
where they are, so non-PDF payloads are unaffected.

Test covers three out-of-order page-1/page-2 blocks: the chunk now reads FIRST,
SECOND, page two.
@1BugKiller

Copy link
Copy Markdown
Contributor Author

The ci label has not been applied, so the sep-tests jobs on this PR are skipped: the workflow triggers on synchronize / labeled and every job is gated on contains(github.event.pull_request.labels.*.name, 'ci').

Could someone add it? Everything else is ready: one commit on main (70ed4c95d), the regression test fails on the parent revision (SECOND … FIRST … instead of reading order) and passes with the change, the existing One tests still pass, and gofmt / go vet / check_files.py are clean. The package's one failing test, TestChunkerGoldenParity, fails on the base revision too — it compares snapshots byte-for-byte and git checks them out with CRLF on Windows.

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The One chunker now sorts PDF-candidate items by page, top, and left before joining their text and positions. A regression test checks that one chunk preserves this reading order.

Changes: PDF reading order

Layer / File(s) Summary
Sort PDF items before joining
internal/ingestion/component/chunker/one.go, internal/ingestion/component/chunker/one_test.go
When position merging is enabled for PDF-candidate input without spreadsheet items, the chunker sorts items before joining text and positions. A test checks the resulting order by page, top, then left.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: xugangqiang, jay77721

Merge Risk: Minimal · up to fa61c

The One chunker now orders PDF blocks by page, top and left before joining them, which matches the General parser and the documented contract. Non-PDF and spreadsheet inputs are unchanged. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix(chunker): assemble the One chunk in reading order" matches the main change. The One chunker now sorts PDF candidate items by reading order before joining their text, which is the primar…
Description check Passed The description contains a summary-style 'What was wrong' and 'The change' section that explains the background, the purpose, and the fix. It links issue #22939 and lists tests and verification steps.…
Linked Issues check Passed [#22939] The only issue requirement is coding: the One chunker must join PDF blocks in (page, top, left) reading order. The diff at head adds src = sortPDFUnits(src) in buildOneChunkFromItems (int…
Out of Scope Changes check Passed The whole-PR diff against the merge base touches one production file (one.go, +7 lines) and one test file (one_test.go, +35 lines, per the summary). The production change is a single guarded sort in `…
Full details: Title check

Explanation

The title "fix(chunker): assemble the One chunk in reading order" matches the main change. The One chunker now sorts PDF candidate items by reading order before joining their text, which is the primary fix described in the raw summary and objectives. The title is specific and concise, and it follows a conventional commit style.

Full details: Description check

Explanation

The description contains a summary-style 'What was wrong' and 'The change' section that explains the background, the purpose, and the fix. It links issue #22939 and lists tests and verification steps. The repository template asks for a Summary section that briefly describes the aim and background. The description gives this context under different headings, and it has no heading named 'Summary'. The content covers the template's intent, so the check passes. A reviewer could add a heading named 'Summary' to match the template exactly.

Full details: Linked Issues check

Explanation

[#22939] The only issue requirement is coding: the One chunker must join PDF blocks in (page, top, left) reading order. The diff at head adds src = sortPDFUnits(src) in buildOneChunkFromItems (internal/ingestion/component/chunker/one.go) before the text join and position merge. The sort runs under mergePositions := pdfCandidate && !hasOneChunkSpreadsheetItems(src). sortPDFUnits is the same shared sorter that chunkPDF in general.go uses. It sorts contiguous runs of positioned units and treats units without coordinates or with PreserveOrder as barriers, which matches the issue's expected behavior. The join uses the sorted src, so the text order follows the sort. The issue asks for a regression test. The PR adds TestOneChunker_KeepsReadingOrderForOutOfOrderPDFItems, which covers the reproduction in the issue. I did not run the tests. The PR reports that the One tests pass and that TestChunkerGoldenParity fails on the base as well. That golden-parity failure is outside this issue's requirements. The non-coding items (CI label, manual steps) do not apply.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base touches one production file (one.go, +7 lines) and one test file (one_test.go, +35 lines, per the summary). The production change is a single guarded sort in buildOneChunkFromItems. It only applies to PDF candidates that are not spreadsheets, and it is inside the multi-item branch. Single-item payloads, Markdown, text, and HTML paths are unchanged. The test directly exercises the change. No unrelated code changes appear in the reviewed diff.

  • 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 reads the pages in line,
First by page, then top, then left,
The scrambled blocks now find their place,
One chunk carries text in order,
And carrots wait beside the file.

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: 8520dd5f-5709-4130-8342-1c0fdb771b7d
Commits

Reviewing files that changed from the base of the PR and between 70ed4c9 and fa61c2f.

Files selected for processing: 2

  • internal/ingestion/component/chunker/one.go
  • internal/ingestion/component/chunker/one_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: The One parser joins PDF blocks in upstream order, scrambling the single chunk

2 participants