Repository navigation
refactor(chunker): replace allowBoundaryOverflow bool with MergeStrategy enum - #17851
yuzhichang merged 3 commits into
Conversation
…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]>
📝 WalkthroughWalkthroughThe chunker replaces boolean merge-mode arguments with an explicit ChangesToken merge strategy
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/ingestion/component/schema/chunker.go (1)
214-220: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winRemove the duplicated
UnderCapdocumentation.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
📒 Files selected for processing (6)
internal/ingestion/component/chunker/token.gointernal/ingestion/component/chunker/token_batch1_test.gointernal/ingestion/component/chunker/token_pdfpos_test.gointernal/ingestion/component/chunker/token_strict_cap_test.gointernal/ingestion/component/schema/chunker.gointernal/ingestion/component/schema/schema_test.go
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()) |
There was a problem hiding this comment.
Need to ensure frontend is able to show and edit the chunker params.
Summary
Follow-up to #17835 (merged). The OVER_CAP / UNDER_CAP merge strategy was threaded through
mergeDecisionandmergeByTokenSizeFromJSONas an inlinedallowBoundaryOverflow boolderived from!c.param.UnderCapat three call sites. This replaces that with a namedschema.MergeStrategyenum.Why
!c.param.UnderCapinversion 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.allowBoundaryOverflow(true = OVER_CAP) is a double-negation ofUnderCapand reads opaquely at the 5th positional argument.What changed
schema.MergeStrategy(MergeOverCap/MergeUnderCap) mirroring Python'srag/nlp/__init__.pyMergeStrategy, so Go and Python stay on the same vocabulary.TokenChunkerParam.MergeStrategy()derived from the wire-facingUnderCap bool(existing"under_cap"configs keep working — no schema break).mergeDecisionandmergeByTokenSizeFromJSONnow takeschema.MergeStrategyinstead ofallowBoundaryOverflow bool; the three call sites passc.param.MergeStrategy()(no!).UnderCap->MergeStrategymapping and an end-to-end test for UNDER_CAP on the JSON path.No behavior change: default remains OVER_CAP,
under_cap=truestill selects UNDER_CAP.Test plan
bash build.sh --test ./internal/ingestion/component/chunker/... ./internal/ingestion/component/schema/...— all green, includingTestMergeByTokenSizeFromJSON_UnderCapNoOverflow,TestMergeByTokenSize_UnderCapNoOverflow,TestInvokeJSONPayload_UnderCapEndToEnd, andTestTokenChunkerParamMergeStrategy.Related issues