Skip to content

fix(chunker): preserve inter-line trailing space in text-path overlap prefix - #17748

Closed
xugangqiang wants to merge 1 commit into
infiniflow:mainfrom
xugangqiang:fix/chunker-overlap-prefix-align
Closed

xugangqiang wants to merge 1 commit into
infiniflow:mainfrom
xugangqiang:fix/chunker-overlap-prefix-align

Conversation

@xugangqiang

Copy link
Copy Markdown
Contributor

Summary

  • Fixes the token-text-overlap-split parity divergence on the text path of TokenChunker.
  • Root cause: mergeByTokenSize applied strings.TrimSpace to each sentence-delimiter-split fragment, dropping the trailing whitespace of each line. The overlap prefix is carved from the untrimmed previous chunk, so that dropped character made every following chunk head diverge from Python's naive_merge by one character (e.g. Go "eta..." vs Python "ta ...", and the chunk boundary "alpha\nbeta" vs "alpha \nbeta").
  • Fix: mirror naive_merge, which builds each unit from "\n" + sub_sec (sub_sec keeps its trailing space) and only strips at the final chunk output. Skip only genuinely empty fragments instead of TrimSpacing each part.

Verification

  • On the token__text_overlap case (chunk_token_size=64, delimiters=["\n"], overlapped_percent=0.1), Go output now matches Python rag/nlp.naive_merge byte-for-byte across all 3 chunks (lengths 239/224/263).
  • Added TestTokenChunker_TextOverlapPreservesInterLineSpace, which fails without the fix and passes with it, and pins the exact chunk texts against the Python reference.
  • Full ./internal/ingestion/component/chunker/ unit suite passes (no regression).

Notes

🤖 Generated with CodeBuddy Code

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. 🧪 test Pull requests that update test cases. labels Aug 3, 2026
@coderabbitai

coderabbitai Bot commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b7cd6273-9a37-4dbe-8a52-99e67345d065

📥 Commits

Reviewing files that changed from the base of the PR and between 883fd20 and 445a697.

📒 Files selected for processing (2)
  • internal/ingestion/component/chunker/token.go
  • internal/ingestion/component/chunker/token_overlap_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • internal/ingestion/component/chunker/token.go
  • internal/ingestion/component/chunker/token_overlap_test.go

📝 Walkthrough

Walkthrough

The token chunker preserves trailing whitespace from sentence-split fragments during merging and overlap calculation. A regression test verifies exact multi-line chunks and Python-compatible overlap boundaries.

Changes

Token overlap whitespace preservation

Layer / File(s) Summary
Preserve and validate overlap whitespace
internal/ingestion/component/chunker/token.go, internal/ingestion/component/chunker/token_overlap_test.go
mergeByTokenSize preserves trailing whitespace and skips only empty fragments. The regression test validates three exact chunks and retains the space before a newline in overlap.

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

Possibly related PRs

Suggested reviewers: yingfeng, yuzhichang, jinhai-cn

Poem

A rabbit checks each token’s place,
And keeps the trailing space in grace.
Newlines hold their overlap true,
Three exact chunks come hopping through.
No whitespace crumb is lost. 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: preserving inter-line trailing spaces during text-path overlap merging.
Description check ✅ Passed The description includes the required Summary section and clearly explains the problem, root cause, fix, verification, and regression test.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch

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

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/ingestion/component/chunker/token_overlap_test.go`:
- Around line 34-44: Update the regression test to exercise mergeByTokenSize
directly with the existing text, token budget, and overlap inputs, rather than
invoking Invoke with a non-custom newline delimiter that routes through
mergeByTokenSizeFromJSON. Keep the assertions and test data focused on the
text-token regression and ensure the changed merge loop is covered.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 396bbef6-cf88-43bf-8f07-41b18a857d45

📥 Commits

Reviewing files that changed from the base of the PR and between 1d141af and 883fd20.

📒 Files selected for processing (2)
  • internal/ingestion/component/chunker/token.go
  • internal/ingestion/component/chunker/token_overlap_test.go

@xugangqiang xugangqiang added the ci Continue Integration label Aug 3, 2026
@xugangqiang

Copy link
Copy Markdown
Contributor Author

@yuzhichang @JinHai-CN @yingfeng Please help to review

… prefix

The text path of TokenChunker split the payload on sentence delimiters
and TrimSpaced each fragment before merging. This dropped the trailing
whitespace of each line, so the overlap prefix carved from the previous
(untrimmed) chunk lost a character and diverged from Python's
naive_merge by one character on every following chunk.

Mirror naive_merge, which builds each unit from "\n" + sub_sec (sub_sec
keeps its trailing space and is never TrimSpaced; the only
post-processing is dropping the leading empty placeholder at
naive_merge:1370-1375). Skip only genuinely empty fragments instead of
TrimSpacing each part.

Fixes the token-text-overlap-split parity divergence. Adds a regression
test that drives Invoke with empty delimiters so it actually lands on the
patched mergeByTokenSize: the text path only reaches that function when
there is no active delimiter, so a non-empty delimiter (e.g. ["\n"])
must NOT be used — it would route to the separate
mergeByTokenSizeFromJSON path, which this fix does not touch. The test
pins the exact chunk texts against the Python reference.
@xugangqiang
xugangqiang force-pushed the fix/chunker-overlap-prefix-align branch from 883fd20 to 445a697 Compare August 4, 2026 11:16
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Aug 4, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Aug 4, 2026
@yuzhichang yuzhichang added ci Continue Integration and removed ci Continue Integration labels Aug 4, 2026
@xugangqiang
xugangqiang marked this pull request as draft August 4, 2026 12:04
@xugangqiang

Copy link
Copy Markdown
Contributor Author

Superseded by the combined Go PR #17835 (inter-line trailing space + OVER_CAP merge sync). Closing in favor of #17835.

@xugangqiang xugangqiang closed this Aug 5, 2026
xugangqiang added a commit to xugangqiang/ragflow that referenced this pull request Aug 5, 2026
Extract a shared mergeDecision helper (mergeAction + allowBoundaryOverflow)
and newChunkText, and rewrite the text (addChunk) and JSON (addTextChunk)
merge paths to use it. Go now matches Python's canonical OVER_CAP default
(rag/nlp MergeStrategy): greedily accumulate adjacent units, allow one
boundary overflow then close the chunk, but never merge a unit that already
exceeds the cap (it stands alone, mirroring Python's `if pt > cap` branch).

Update the cap/overlap tests to the OVER_CAP contract (a chunk may exceed
budget by at most one incoming unit) and restructure the overlap-tag test to
exercise the overlap path under OVER_CAP.

Part of consolidating infiniflow#17744/infiniflow#17748 into a single Go PR that syncs the chunker
with Python PR infiniflow#17808 (OVER_CAP default).
@xugangqiang
xugangqiang deleted the fix/chunker-overlap-prefix-align branch August 12, 2026 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files. 🧪 test Pull requests that update test cases.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants