Repository navigation
fix(chunker): merge group records against chunk_token_cap, not 1024 - #22948
Conversation
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.
|
The Could someone add it? Everything else is ready: one commit on |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
LGTM |
yuzhichang
left a comment
There was a problem hiding this comment.
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.pybefore the Python tree was removed):cap = self.param.chunk_token_cap or 0, thenmerge_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.
Fixes #22947
What was wrong
The group chunker merged against a hard-coded
maxGroupTokens = 1024, while the reference takes the ceiling from the configuration:MIN_GROUP_TOKENSis the only constant the reference still has;MAX_GROUP_TOKENSdoes 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 thenenforceTitleTokenCapre-split at sentence boundaries, so the boundaries differed from both the configuration and the reference.The change
groupRecordsnow usesp.ChunkTokenCapwith the reference's rule (cap <= 0means no ceiling);maxGroupTokensis gone.ManualChunkershares the grouping body but stripschunk_token_capfrom 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 explicitmanualMergeCeiling = 1024, keeping its output byte-identical to before.Rejecting
chunk_token_cap = 0stays as decided in #22915; thecap <= 0branch is kept for parity with the reference and for internal callers such asManualChunker's exempt path.Tests
internal/ingestion/component/chunker/group_test.go:TestGroupChunker_MergeCeilingFollowsChunkTokenCap—chunk_token_cap = 2048with a ~1800-token section must be one chunk. Before:got 2 chunks, want 1.go test ./internal/ingestion/component/chunker/ -run 'TestGroup|TestManual'→ ok, includingTestManualChunker_NoPositionsEqualsGroupChunkerand the manual position-sort tests.Verification
TestChunkerGoldenParityfails on this base as well: it compares snapshots byte-for-byte and git checks them out with CRLF on Windows. Verified pre-existing onmain.gofmt,go vetand the repo'stools/hooks/check_files.pychecks (eof, trailing whitespace, mixed line endings, ASCII comments, case conflicts, merge conflicts) are clean.