Skip to content

fix(chunker): keep oversize text/markdown unit whole (OVER_CAP alignment) - #17854

Merged
yuzhichang merged 4 commits into
infiniflow:mainfrom
xugangqiang:fix/chunker-text-overcap-align
Aug 5, 2026
Merged

yuzhichang merged 4 commits into
infiniflow:mainfrom
xugangqiang:fix/chunker-text-overcap-align

Conversation

@xugangqiang

@xugangqiang xugangqiang commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

What

The Go TokenChunker text/markdown path (mergeByTokenSize) unconditionally
called splitOversizedUnit on any unit that exceeded chunk_token_size,
emitting Go-only sub-chunks. Python's naive_merge (_merge_paragraph_groups,
rag/nlp/__init__.py) never atom-splits an oversize unit under either
OVER_CAP or UNDER_CAP: a paragraph larger than the budget becomes its own
standalone chunk and the model layer truncates it later.

This aligns the text/markdown path with the structured JSON path
(invokeJSONPayload → mergeByTokenSizeFromJSON(..., subSplitOversize=false),
#17739). It completes the OVER_CAP alignment started in #17835.

Cases fixed (chunker parity)

  • token__text_long_paragraph: Python 1 chunk, Go was 3 → now 1.
  • token__markdown_long: Python 1 chunk, Go was 4 → now 1.

How

An oversize unit is passed through addChunk, which mergeDecision already
forces to startNewChunk (token.go:514) when the incoming unit exceeds the
target, so it stands alone exactly like Python. No atom-split on the
text/markdown path. The addUnit wrapper was inlined into addChunk.

Note

This is one step of the broader chunker-parity alignment. Remaining divergences
are tracked separately:

…ent)

Python's naive_merge (_merge_paragraph_groups, rag/nlp/__init__.py) never
atom-splits an oversize unit under either OVER_CAP or UNDER_CAP: a paragraph
larger than chunk_token_size becomes its own standalone chunk and the model
layer truncates it later. The Go text/markdown path (mergeByTokenSize) instead
called splitOversizedUnit unconditionally, emitting Go-only sub-chunks that
diverged from the Python reference (parity cases token__text_long_paragraph
and token__markdown_long: Python 1 chunk, Go 3-4).

Pass the oversize unit through addChunk, which mergeDecision already forces to
startNewChunk (token.go:514), so it stands alone exactly like Python. The JSON
path already does this via subSplitOversize=false (infiniflow#17739); this completes the
OVER_CAP alignment on the text/markdown path.

Add TestTokenChunker_OversizeUnitKeptWhole to pin the contract: a single
unbroken line with no delimiter yields one standalone chunk on both the text
and markdown paths.

Co-authored-by: xugangqiang <>
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. labels Aug 5, 2026
@coderabbitai

coderabbitai Bot commented Aug 5, 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: 861029a5-04cf-4fc5-96f6-9615b689937a

📥 Commits

Reviewing files that changed from the base of the PR and between e20bbec and 5eae9cf.

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

📝 Walkthrough

Walkthrough

The token chunker now retains oversized text and Markdown units as standalone chunks. New Go tests cover standalone oversized paragraphs and oversized paragraphs that follow in-budget paragraphs.

Changes

Oversized token retention

Layer / File(s) Summary
Whole oversized unit handling
internal/ingestion/component/chunker/token.go, internal/ingestion/component/chunker/token_oversize_whole_test.go
The token merger adds oversized units directly instead of atom-splitting them. Tests verify whole chunks for text and Markdown inputs, including an oversized paragraph after an in-budget paragraph.

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

Possibly related issues

Possibly related PRs

Suggested labels: 🧪 test

Suggested reviewers: jinhai-cn, yingfeng, yuzhichang

Poem

I’m a rabbit with chunks in a row,
Oversized ones stay whole as they grow.
Text and Markdown pass,
No splits through the grass,
One neat chunk is ready to go.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the main change: preserving oversized text and Markdown units as whole chunks.
Description check ✅ Passed The description explains the problem, parity goal, affected cases, implementation, and remaining scope, but it does not use the template's Summary heading.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

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

🧹 Nitpick comments (1)
internal/ingestion/component/chunker/token.go (1)

613-621: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Remove migration-history details from these comments.

Keep the current runtime contract and remove references to previous Go behavior and named parity cases.

  • internal/ingestion/component/chunker/token.go#L613-L621: describe that oversized units pass to addChunk and start a new chunk.
  • internal/ingestion/component/chunker/token_oversize_whole_test.go#L23-L30: describe the current regression case only.

As per coding guidelines, keep package and doc comments aligned with the current runtime path rather than migration history.

🤖 Prompt for 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.

In `@internal/ingestion/component/chunker/token.go` around lines 613 - 621, Update
the comments at internal/ingestion/component/chunker/token.go#L613-L621 to
describe only the current behavior: oversized units are passed to addChunk,
which starts a new chunk; remove references to prior Go behavior, Python
implementation details, and parity cases. Also revise the comment at
internal/ingestion/component/chunker/token_oversize_whole_test.go#L23-L30 to
describe only the current regression case, without migration-history details.

Source: Coding guidelines

🤖 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_oversize_whole_test.go`:
- Around line 55-71: Add a regression case to
TestTokenChunker_OversizeUnitKeptWhole with an in-budget unit before the
oversized unit and a sentenceDelimiter-accepted separator, then assert the
output contains two chunks with the expected text so the addChunk merge path
invokes mergeDecision.

---

Nitpick comments:
In `@internal/ingestion/component/chunker/token.go`:
- Around line 613-621: Update the comments at
internal/ingestion/component/chunker/token.go#L613-L621 to describe only the
current behavior: oversized units are passed to addChunk, which starts a new
chunk; remove references to prior Go behavior, Python implementation details,
and parity cases. Also revise the comment at
internal/ingestion/component/chunker/token_oversize_whole_test.go#L23-L30 to
describe only the current regression case, without migration-history details.
🪄 Autofix

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: 251e7ebb-87ca-4625-b992-a789db15d401

📥 Commits

Reviewing files that changed from the base of the PR and between 3102752 and 8337695.

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

…h test

Follow-ups to infiniflow#17854 from review:
- Drop stale doc comment claiming oversize units are atom-split via
  splitOversizedUnit; they are now kept whole (Python OVER_CAP).
- Inline the now-redundant addUnit wrapper into addChunk.
- Add TestTokenChunker_OversizeUnitStandsAloneAfterInBudgetUnit, which
  exercises mergeDecision's oversize branch (in-budget unit followed by
  an oversize paragraph) that the lone-oversize test never reached.
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Aug 5, 2026

@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_oversize_whole_test.go`:
- Around line 128-131: Update the first-chunk assertion in the token oversize
test to require exact equality with inBudget rather than substring containment,
while preserving the existing failure context.
🪄 Autofix

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: f982e556-6a0f-4d59-b250-8f7e363b17be

📥 Commits

Reviewing files that changed from the base of the PR and between 8337695 and 5509507.

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

xugangqiang and others added 2 commits August 5, 2026 17:03
…served

- Remove TestMergeByTokenSize_UnbrokenAtomStrictCap: it pinned the removed
  atom-split (splitOversizedUnit) behavior, which blocked the unit tier.
- Trim migration-narrative doc comment in TestTokenChunker_OversizeUnitKeptWhole.
- Strengthen TestTokenChunker_OversizeUnitKeptWhole to assert the oversize
  unit's content is preserved (not just the chunk count), comparing trailing
  whitespace on both sides.

Co-authored-by: CodeBuddy <[email protected]>
Strengthen TestTokenChunker_OversizeUnitStandsAloneAfterInBudgetUnit: use
exact equality (first != inBudget) instead of substring containment, so the
test catches a regression where the oversize paragraph is wrongly merged into
the previous chunk. The final-output TrimSpace (token.go:657) guarantees first
is exactly "Hello world.".

Co-authored-by: CodeBuddy <[email protected]>
@yuzhichang

Copy link
Copy Markdown
Member

LGTM

@yuzhichang
yuzhichang merged commit 2fcc349 into infiniflow:main Aug 5, 2026
3 checks passed
yuzhichang added a commit to yuzhichang/ragflow that referenced this pull request Aug 6, 2026
…OVER_CAP)

The EmbeddingModel BatchSize port (infiniflow#17881) landed on a pre-infiniflow#17854 base and
dropped the OVER_CAP alignment: mergeByTokenSize still atom-split oversize
units via splitOversizedUnit, and the old token_strict_cap_test.go (which
covered the removed strict-cap behavior) was left behind while the new
token_oversize_whole_test.go was dropped.

Restore infiniflow#17854:
- mergeByTokenSize routes oversize units through addChunk and keeps them
  whole as standalone chunks (Python OVER_CAP; model layer truncates later)
  instead of calling splitOversizedUnit.
- Add token_oversize_whole_test.go, delete token_strict_cap_test.go.

Co-authored-by: Jack <[email protected]>
@xugangqiang
xugangqiang deleted the fix/chunker-text-overcap-align branch August 7, 2026 12:27
xugangqiang added a commit to xugangqiang/ragflow that referenced this pull request Aug 10, 2026
After rebasing onto upstream/main (which merged infiniflow#17739/infiniflow#17835/infiniflow#17854/infiniflow#17868):
- Remove 4 go_bug rules now resolved by merged fixes:
  token-json-per-item-merge, token-json-custom-delim-drops-newline,
  token-json-single-item-oversize, token-text-overlap-split.
- Delete their go_snapshot files; also drop 4 orphaned tokenizer
  go_snapshot files that the harness never reads (strict-compare path).
- Refresh the 3 remaining ratcheted snapshots for
  token-custom-delim-newline-and-field (html/markdown/text_backtick), whose
  Go output shifted under the merged delimiter/children-split fixes but the
  divergence itself persists.

The 4 tokenizer-count cases (text/html/markdown_default_delim,
text_token_liveness) now match the Python golden strict-compare with no
rule, i.e. the tokenizer count mismatch is resolved. Harness is green under
both plain and -update runs.
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 size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants