Skip to content

fix(chunker): merge group records against chunk_token_cap, not 1024 - #22948

Merged
yuzhichang merged 1 commit into
infiniflow:mainfrom
1BugKiller:fix/group-merge-ceiling
Nov 30, 2026
Merged

yuzhichang merged 1 commit into
infiniflow:mainfrom
1BugKiller:fix/group-merge-ceiling

Conversation

@1BugKiller

Copy link
Copy Markdown
Contributor

Fixes #22947

What was wrong

The group chunker merged against a hard-coded maxGroupTokens = 1024, while the reference takes the ceiling from the configuration:

# group_chunker.py
cap = self.param.chunk_token_cap or 0
merge_ceiling_ok = cap <= 0 or tk_cnt < cap

MIN_GROUP_TOKENS is the only constant the reference still has; MAX_GROUP_TOKENS does not exist in it.

With one ~1800-token section:

  • chunk_token_cap = 2048 → Python: one chunk; before this change Go produced two.
  • chunk_token_cap = 256 → Python stops at 256 and record boundaries decide; Go merged to 1024 first and then enforceTitleTokenCap re-split at sentence boundaries, so the boundaries differed from both the configuration and the reference.

The change

groupRecords now uses p.ChunkTokenCap with the reference's rule (cap <= 0 means no ceiling); maxGroupTokens is gone.

ManualChunker shares the grouping body but strips chunk_token_cap from its config (it is exempt from the title-family cap), so it would have silently inherited the schema default of 512. It now passes an explicit manualMergeCeiling = 1024, keeping its output byte-identical to before.

Rejecting chunk_token_cap = 0 stays as decided in #22915; the cap <= 0 branch is kept for parity with the reference and for internal callers such as ManualChunker's exempt path.

Tests

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

  • TestGroupChunker_MergeCeilingFollowsChunkTokenCap — chunk_token_cap = 2048 with a ~1800-token section must be one chunk. Before: got 2 chunks, want 1.

go test ./internal/ingestion/component/chunker/ -run 'TestGroup|TestManual' → ok, including TestManualChunker_NoPositionsEqualsGroupChunker and the manual position-sort tests.

Verification

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 group chunker takes its merge ceiling from the configuration
(group_chunker.build_chunks: `cap = self.param.chunk_token_cap or 0`, so a
non-positive cap means no ceiling). The Go port merged against a hard-coded
maxGroupTokens = 1024 instead, a constant the reference no longer has.

So a cap above 1024 split sections earlier than configured — chunk_token_cap
2048 produced two chunks for a 1800-token section — and a cap below 1024 merged
records past the ceiling and let the post-build enforceTitleTokenCap re-split
them at sentence boundaries rather than at record boundaries.

Group now uses p.ChunkTokenCap with the reference's `cap <= 0 means no ceiling`
rule; maxGroupTokens is gone.

ManualChunker shares the same grouping body but has no cap configuration of its
own (it strips chunk_token_cap from its config), so it keeps the previous ceiling
through an explicit manualMergeCeiling = 1024 instead of inheriting the schema
default of 512. Its output is unchanged.

Test: a ~1800-token section with chunk_token_cap=2048 is one chunk; before it
was 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 (got 2 chunks, want 1) and passes with the change, the group/manual 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 29, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

GroupTitleChunker now uses chunk_token_cap as its same-section merge ceiling. ManualChunker sets an explicit 1024-token ceiling when it calls the shared grouping function.

Changes: Group Chunking

Layer / File(s) Summary
Configured group merge ceiling
internal/ingestion/component/chunker/group.go, internal/ingestion/component/chunker/group_test.go
GroupTitleChunker uses ChunkTokenCap for same-section merging. A non-positive cap disables that ceiling, while the minimum-token rule still permits merging across sections. A test checks that a 2048-token cap keeps an approximately 1800-token section in one chunk.
ManualChunker ceiling
internal/ingestion/component/chunker/manual.go
ManualChunker copies its parameters, sets ChunkTokenCap to 1024, and passes them to chunkFromRecords with tokenCap=0.

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

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

  • infiniflow/ragflow#18272: Added ManualChunker and the shared grouping path that this change updates.
  • infiniflow/ragflow#22915: Changed the chunk_token_cap contract and introduced the tokenCap=0 sentinel used by ManualChunker.
  • infiniflow/ragflow#20219: Passed chunk_token_cap into the shared ManualChunker path, which this change now gives an explicit 1024-token ceiling.

Suggested reviewers: xugangqiang, jay77721

Merge Risk: Minimal · up to 65e93

The group chunker now follows the configured chunk_token_cap for merging, and the manual chunker keeps its previous 1024-token behavior. No concrete merge-blocking risk was found. The remaining caveat is a snapshot test that reportedly also fails on the base revision.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the group chunker merge ceiling now follows chunk_token_cap instead of the hard-coded 1024. It also carries the conventional fix(chunker) prefix. The changeset matches…
Description check Passed The description states the problem, the reference rule, and the change. It lists the affected cases, the ManualChunker handling, the tests run, and the verification results. It does not follow the rep…
Linked Issues check Passed [#22947] requires the group chunker merge ceiling to come from chunk_token_cap, with cap <= 0 meaning no ceiling. At head, group.go removes maxGroupTokens. groupRecords now sets cap := p.ChunkTokenCap…
Out of Scope Changes check Passed All reported changes relate to #22947. group.go changes the merge ceiling source and adds the manualMergeCeiling constant. manual.go, per the PR summary, passes an explicit 1024 ceiling so ManualChunk…
Full details: Title check

Explanation

The title names the main change: the group chunker merge ceiling now follows chunk_token_cap instead of the hard-coded 1024. It also carries the conventional fix(chunker) prefix. The changeset matches this: group.go drops maxGroupTokens and uses ChunkTokenCap, manual.go pins an explicit 1024 ceiling, and a regression test is added.

Full details: Description check

Explanation

The description states the problem, the reference rule, and the change. It lists the affected cases, the ManualChunker handling, the tests run, and the verification results. It does not follow the repository template, which asks for a Summary section. The content covers the same purpose, though, so the description is mostly complete.

Full details: Linked Issues check

Explanation

[#22947] requires the group chunker merge ceiling to come from chunk_token_cap, with cap <= 0 meaning no ceiling. At head, group.go removes maxGroupTokens. groupRecords now sets cap := p.ChunkTokenCap and uses mergeCeilingOK := cap <= 0 || tkCnt < cap in the same-section merge condition. This matches the reference rule in group_chunker.py. The 32-token minimum rule is kept (minGroupTokens). The issue asks for ManualChunker to keep its prior 1024 ceiling explicitly. group.go declares manualMergeCeiling = 1024, and the PR summary states that manual.go passes it. I could not read the manual.go diff because the changed blob was unavailable, so the manual.go wiring rests on the PR summary and the declared constant. The regression test TestGroupChunker_MergeCeilingFollowsChunkTokenCap is reported in group_test.go. I did not read that test diff. The core coding requirement is met by the group.go code at head.

Full details: Out of Scope Changes check

Explanation

All reported changes relate to #22947. group.go changes the merge ceiling source and adds the manualMergeCeiling constant. manual.go, per the PR summary, passes an explicit 1024 ceiling so ManualChunker output stays unchanged. This is the protection the issue requires. group_test.go adds a regression test for the cap source. Nothing in the reviewed group.go diff falls outside the issue scope. The manual.go and group_test.go diffs could not be read directly, so their scope is based on the PR summary.

  • 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 token line,
“Two thousand fits this chunk just fine!”
The sections merge beneath the cap,
While Manual keeps its measured map.
The rabbit hops, pleased with the change.

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: 50a03616-874c-4ab0-90d9-e5a0eebcfe6a
Commits

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

Files selected for processing: 3

  • internal/ingestion/component/chunker/group.go
  • internal/ingestion/component/chunker/group_test.go
  • internal/ingestion/component/chunker/manual.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.

@wangq8
wangq8 requested a review from xugangqiang November 29, 2026 13:01
@xugangqiang xugangqiang added the ci Continue Integration label Nov 30, 2026
@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.

Review: fix(chunker): merge group records against chunk_token_cap, not 1024

Verified against base 70ed4c95d.

Correctness

The group merge condition now matches the reference implementation exactly:

  • Python (rag/flow/chunker/title_chunker/group_chunker.py before the Python tree was removed): cap = self.param.chunk_token_cap or 0, then merge_ceiling_ok = cap <= 0 or tk_cnt < cap.
  • Head (internal/ingestion/component/chunker/group.go:254-258): cap := p.ChunkTokenCap; mergeCeilingOK := cap <= 0 || tkCnt < cap.

The cap <= 0 branch is unreachable from production config (TitleChunkerParam.Validate rejects anything outside 128..8000 — internal/ingestion/component/schema/chunker.go:422), but keeping it mirrors the reference and keeps hand-built params (tests) working. No caller of groupRecords had to change: invokeGroup is the only production entry and already passes p.

ManualChunker

manual.go:129-131 copies c.param and sets ChunkTokenCap = manualMergeCeiling (1024) for the shared grouping body only. That is a faithful preservation of the pre-change behaviour: the old shared body used the hard-coded maxGroupTokens = 1024 for both variants, and Python's rag/app/manual.py used tk_cnt < 1024. Because the copy is local, ManualChunker's stored param stays at the schema default (512), which manual_test.go:485 still asserts. The post-build cap remains disabled for manual via the separate tokenCap=0 argument.

Behaviour change (intentional)

GroupTitleChunker with default config (chunk_token_cap = 512) now stops merging a section at 512 tokens instead of 1024, so a long un-subheaded section is split at record boundaries instead of being merged to ~1024 and then re-split at sentence boundaries by enforceTitleTokenCap. That is the Python contract this PR restores; chunk counts and boundaries for such sections change. ManualChunker keeps 1024, so Group and Manual now diverge for sections between 512 and 1024 tokens — documented in the new comment at manual.go:124-128. Note that the docstring of TestManualChunker_NoPositionsEqualsGroupChunker (manual_test.go:128-132) still says any divergence is an unintended regression; that sentence is now stale, but the test's fixture is short enough that it still holds.

Test

TestGroupChunker_MergeCeilingFollowsChunkTokenCap is a genuine regression test: with cap=2048 the ~1800-token section merges into one chunk only if the ceiling is read from config; the base code (1024) splits it into two. It fails on the base revision and passes on head.

No blocking findings.

@yuzhichang
yuzhichang merged commit 0f80156 into infiniflow:main Nov 30, 2026
7 checks passed
@1BugKiller
1BugKiller deleted the fix/group-merge-ceiling branch November 30, 2026 06:23
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.

[Bug]: The group chunker merges against a hard-coded 1024 ceiling instead of chunk_token_cap

3 participants