Skip to content

refactor(chunker): replace allowBoundaryOverflow bool with MergeStrategy enum - #17851

Merged
yuzhichang merged 3 commits into
infiniflow:mainfrom
xugangqiang:refactor/chunker-merge-strategy-enum
Aug 5, 2026
Merged

yuzhichang merged 3 commits into
infiniflow:mainfrom
xugangqiang:refactor/chunker-merge-strategy-enum

Conversation

@xugangqiang

@xugangqiang xugangqiang commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #17835 (merged). The OVER_CAP / UNDER_CAP merge strategy was threaded through mergeDecision and mergeByTokenSizeFromJSON as an inlined allowBoundaryOverflow bool derived from !c.param.UnderCap at three call sites. This replaces that with a named schema.MergeStrategy enum.

Why

  • The !c.param.UnderCap inversion was hand-written in three places, so a future strategy addition could silently drift between the JSON path (invokeTextPayload / invokeJSONPayload) and the text path (mergeByTokenSize) — no compile error, and the existing tests don't cover all three sites with both strategies.
  • The strategy concept was never named; allowBoundaryOverflow (true = OVER_CAP) is a double-negation of UnderCap and reads opaquely at the 5th positional argument.

What changed

  • Add schema.MergeStrategy (MergeOverCap / MergeUnderCap) mirroring Python's rag/nlp/__init__.py MergeStrategy, so Go and Python stay on the same vocabulary.
  • Expose TokenChunkerParam.MergeStrategy() derived from the wire-facing UnderCap bool (existing "under_cap" configs keep working — no schema break).
  • mergeDecision and mergeByTokenSizeFromJSON now take schema.MergeStrategy instead of allowBoundaryOverflow bool; the three call sites pass c.param.MergeStrategy() (no !).
  • Tests updated to pass the enum; added a guard test for the UnderCap -> MergeStrategy mapping and an end-to-end test for UNDER_CAP on the JSON path.

No behavior change: default remains OVER_CAP, under_cap=true still selects UNDER_CAP.

Test plan

bash build.sh --test ./internal/ingestion/component/chunker/... ./internal/ingestion/component/schema/... — all green, including TestMergeByTokenSizeFromJSON_UnderCapNoOverflow, TestMergeByTokenSize_UnderCapNoOverflow, TestInvokeJSONPayload_UnderCapEndToEnd, and TestTokenChunkerParamMergeStrategy.

Related issues

…egy enum

The OVER_CAP/UNDER_CAP merge strategy was threaded through mergeDecision and
mergeByTokenSizeFromJSON as an inlined `allowBoundaryOverflow bool` derived
from `!c.param.UnderCap` at three call sites. That double-negation is fragile:
the inversion is hand-written in three places, so a future strategy addition
can silently drift between the JSON path (invokeTextPayload / invokeJSONPayload)
and the text path (mergeByTokenSize). It also never named the strategy concept.

Introduce schema.MergeStrategy (MergeOverCap / MergeUnderCap) mirroring
Python's rag/nlp/__init__.py MergeStrategy, and expose it via
TokenChunkerParam.MergeStrategy() (derived from the wire-facing UnderCap bool,
so existing "under_cap" configs keep working). Call sites now pass
c.param.MergeStrategy() instead of !c.param.UnderCap, and both helpers take the
enum. This removes the triple inversion, makes the strategy self-documenting at
each call site, and gives compile-time safety for future strategies.

Tests updated to pass the enum; added a guard test for the UnderCap ->
MergeStrategy mapping.

Co-authored-by: CodeBuddy <[email protected]>
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Aug 5, 2026
@coderabbitai

coderabbitai Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The chunker replaces boolean merge-mode arguments with an explicit schema.MergeStrategy enum. TokenChunkerParam derives the strategy from UnderCap, and text, JSON, and related tests use the new contract.

Changes

Token merge strategy

Layer / File(s) Summary
Merge strategy contract
internal/ingestion/component/schema/chunker.go, internal/ingestion/component/schema/schema_test.go
Adds MergeStrategy, MergeOverCap, MergeUnderCap, and TokenChunkerParam.MergeStrategy(). Tests cover both UnderCap mappings.
Text and JSON merge paths
internal/ingestion/component/chunker/token.go
Passes explicit merge strategies through text and structured JSON merging while preserving over-cap and under-cap behavior.
Merge behavior test updates
internal/ingestion/component/chunker/*test.go
Updates tests to use explicit strategies for overlap, boundary, strict-cap, PDF metadata, oversized-unit, and end-to-end JSON cases.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested labels: 🧪 test

Suggested reviewers: jinhai-cn, yingfeng, yuzhichang

Poem

I’m a rabbit with a strategy map,
Over-cap hops avoid the gap.
Under-cap paths split with care,
JSON and text now travel fair.
Tests guard every token stair. 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly and concisely describes the main change: replacing a boolean overflow parameter with a MergeStrategy enum.
Description check ✅ Passed The description includes the required Summary section and provides clear context, rationale, implementation details, compatibility notes, and test coverage.

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.

@xugangqiang xugangqiang added the ci Continue Integration label 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.

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

214-220: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Remove the duplicated UnderCap documentation.

Lines 214-220 repeat lines 207-213. Keep the wire-field mapping note in lines 221-222 after the existing contract description.

As per coding guidelines, remove stale documentation.

🤖 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/schema/chunker.go` around lines 214 - 220,
Remove the duplicated UnderCap merge-strategy documentation in the surrounding
chunker configuration comments, preserving the existing contract description and
the wire-field mapping note that follows it.

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.

Nitpick comments:
In `@internal/ingestion/component/schema/chunker.go`:
- Around line 214-220: Remove the duplicated UnderCap merge-strategy
documentation in the surrounding chunker configuration comments, preserving the
existing contract description and the wire-field mapping note that follows it.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: dd34fa81-6fa5-43fb-a7ae-17a52c79f75a

📥 Commits

Reviewing files that changed from the base of the PR and between 510e119 and 2113553.

📒 Files selected for processing (6)
  • internal/ingestion/component/chunker/token.go
  • internal/ingestion/component/chunker/token_batch1_test.go
  • internal/ingestion/component/chunker/token_pdfpos_test.go
  • internal/ingestion/component/chunker/token_strict_cap_test.go
  • internal/ingestion/component/schema/chunker.go
  • internal/ingestion/component/schema/schema_test.go

xugangqiang and others added 2 commits August 5, 2026 14:29
The earlier commit accidentally re-inserted the full UnderCap doc block,
leaving it duplicated above the field. Collapse to a single copy; behavior
and the new MergeStrategy enum are unchanged.

Co-authored-by: CodeBuddy <[email protected]>
Add TestInvokeJSONPayload_UnderCapEndToEnd: many single-line JSON items are
flattened into one global merge sequence and under_cap=true must keep every
chunk within chunk_token_size. The OVER_CAP control asserts it packs fewer
chunks than UNDER_CAP (overflowing by at most one unit), proving the toggle is
live on the JSON path — the text-path equivalent was already covered by
TestInvokeTextPayload_StrictCapEndToEnd.

Co-authored-by: CodeBuddy <[email protected]>
// chunk_token_size with optional overlap.
perItem := [][]schema.ChunkDoc{docs}
merged := mergeByTokenSizeFromJSON(perItem, c.param.ChunkTokenSize, c.param.OverlappedPercent, true, !c.param.UnderCap)
merged := mergeByTokenSizeFromJSON(perItem, c.param.ChunkTokenSize, c.param.OverlappedPercent, true, c.param.MergeStrategy())

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.

Need to ensure frontend is able to show and edit the chunker params.

@yuzhichang
yuzhichang merged commit 3102752 into infiniflow:main Aug 5, 2026
3 checks passed
@xugangqiang
xugangqiang deleted the refactor/chunker-merge-strategy-enum branch August 7, 2026 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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