Repository navigation
fix(chunk): count merge budget in runes, not bytes - #20690
Conversation
mergeChunks accumulated the running buffer length via strings.Builder.Len() (bytes) but compared it against the rune-based target_size and the rune-based per-chunk overflow check. For CJK text every rune is 3 bytes, so the budget was effectively divided by three: chunks flushed early and every merged chunk stayed far below the configured target_size. Track the buffer length in runes alongside the builder so all three checks (the per-chunk overflow, the merge budget, and the flush threshold) operate in the same unit. ASCII behaviour is unchanged. Adds regression tests covering CJK merge budget, ASCII behaviour, and the oversize-unit path for multi-byte text.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to Chunk merging now measures its budget in runes as intended. The inspected code and regression assertions show no concrete merge-blocking risk. Pre-merge checks |
|
There was a problem hiding this comment.
LGTM — the fix is correct, and the byte/rune mix-up in mergeChunks is properly resolved.
One suggestion before this lands: please pin the unit on the config itself.
ChunkOptions.MergeTargetSize in internal/parser/chunk/options.go carries no unit annotation, while the field right below it is explicit:
// Postprocess configuration. Zero values mean "do not run that
// step"; non-zero values enable it.
MergeTargetSize int
// FilterMinLength > 0 drops chunks shorter than that (rune count).
FilterMinLength intThe mixed-unit expression this PR fixes could survive for so long precisely because nothing stated which unit target_size is in, so a byte count and a rune count were allowed to sit on opposite sides of > unnoticed. After this change every size threshold in the package is rune-based (FilterMinLength is documented as "(rune count)", defaultLengthChunkSize as "in runes", splitByLength as "by rune count (not bytes)", and now mergeChunks), so it is worth writing that invariant down:
// MergeTargetSize > 0 enables the merge step; its value is the
// per-chunk budget in runes (Unicode code points).
MergeTargetSize intNot blocking — this is only about making the invariant explicit, and it also settles the point the description has to argue for indirectly, namely that target_size is a rune budget rather than a token budget.
|
Thanks, @zpf121 — addressed in 5b532e0.
Validation on this head:
For visibility, the previous head's Go integration run fails in CI update for this head (run 38024391980):
The earlier upstream-main evidence is from the non-MultiTurn sibling test, not an independent reproduction of this exact MultiTurn failure. I have kept the patch focused on chunk accounting and the requested documentation. Could a maintainer rerun the failed job? |
zpf121
left a comment
There was a problem hiding this comment.
LGTM — approving.
Verified locally on 5b532e0b3:
go test ./internal/parser/chunk/— 30/30 pass, stable at-count=20, clean under-race, coverage 76.4%go vetandgofmt -lclean- The new regression test fails on the pre-fix
postprocess.goand passes after it, and only that CJK case flips: the ASCII and oversize-single-chunk cases are unaffected
The unit annotation on MergeTargetSize addresses the earlier review point — thanks for the quick turnaround.
Two non-blocking follow-ups, neither of which belongs in this PR:
ChunkData.Sizeis still assignedlen(content)(bytes) inPostprocessOperator.Execute, which overrides the rune countsplitByLengthsets. Same root cause as this bug, worth a separate fix.- CI on this run:
ragflow_go_integrationfailed after 10s (looks like a setup/infra failure rather than a test failure — logs were not yet available while the run was still in progress), andragflow_tests_elasticsearch/ragflow_tests_infinityare still pending. The earlierragflow_tests_elasticsearchfailure was the pre-existing flakyTestRunAgent_AllFixture_LoopInterruptResume_MultiTurnininternal/service, which also fails onmain(e.g.1673c3e61) and is unrelated to this change.
Summary
mergeChunksmixed length units: the running buffer total came fromstrings.Builder.Len()(bytes) whiletarget_sizeand the per-chunk overflow check count runes. For CJK text every rune is 3 bytes, so the effective merge budget wastarget_size / 3— the merge flushed early and every merged chunk stayed far below the configured size.The fix tracks the buffer length in runes alongside the builder, so all three checks (per-chunk overflow, merge budget, flush threshold) operate in the same unit. ASCII behaviour is unchanged.
Root cause
In
internal/parser/chunk/postprocess.go:len([]rune(c.Content)) >= target— rune based;target_sizeis documented as a rune count;buf.Len(), which is bytes.nextLenwas computed in runes and then added to a byte count, so for multi-byte text the budget checkbuf.Len()+nextLen+1 > targetfired roughly 3x too early for CJK.Preserved invariant
target_sizeremains a rune-based budget everywhere;+1per join) and metadata merge semantics are unchanged;TestMergeChunksASCIIBehaviourUnchanged).Tests
Regression test fails on unmodified
main(verified on worktree atebb71552a):After the fix:
go test ./internal/parser/chunk/ -count=1passes (full package),go vetclean,gofmtclean.internal/parser/parserpackage does not build in my environment (missingoffice_oxide.hC header forgithub.com/yfedoseev/office_oxide); this is a pre-existing environment limitation unrelated to this change — the touched package builds and passes fully.The original fix was rebased onto
mainatebb71552abefore submission. The review follow-up only documents the rune budget onChunkOptions.MergeTargetSizeinoptions.go; the full diff is limited topostprocess.go,postprocess_test.go, andoptions.go.Risk
Low: single function, unit-only change, no API or persistence impact. Chunks for non-ASCII text will now actually reach the configured
target_size(visible behaviour change for CJK users, which is the point of the fix); ASCII paths are pinned by tests.