Skip to content

fix(chunk): keep postprocessed sizes in runes - #20747

Closed
kassdinzheng123 wants to merge 1 commit into
infiniflow:mainfrom
kassdinzheng123:postprocess-size-counts-runes
Closed

kassdinzheng123 wants to merge 1 commit into
infiniflow:mainfrom
kassdinzheng123:postprocess-size-counts-runes

Conversation

@kassdinzheng123

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #20690, addressing zpf121's separate follow-up about ChunkData.Size.

Count final chunk sizes in Unicode code points (runes), rather than bytes, when PostprocessOperator.Execute assigns ChunkData.Size. This keeps the reported size aligned with the length splitter and the rune-based merge/filter limits. ASCII sizes are unchanged.

Root cause

splitByLength sets Size to the number of runes in each window. Postprocessing subsequently overwrites it with len(GetContent()), a byte count, even when a filter leaves the content untouched. A CJK length split with sizes 2/2/1 therefore becomes 6/6/3 after postprocessing.

The same mismatch is visible through the typed chunk.Run entry point used by the CLI: merging 甲乙 and 丙丁 produces 甲乙 丙丁, which is 5 runes but was reported as size 13.

The fix uses utf8.RuneCountInString at the existing assignment, without changing how chunks are split, merged, or filtered.

Preserved behavior and scope

  • Chunk content, ordering, indices, metadata, merge boundaries, and filter decisions are unchanged.
  • Size counts code points, not UTF-8 bytes, grapheme clusters, or model tokens.
  • This patch fixes postprocessing's size assignment only. It does not populate currently unset sizes in split-only sentence/character/paragraph paths.
  • The observed consumer of this field is the CLI's chunk-result display. This PR does not claim to fix production ingestion, embedding, or retrieval behavior.

Tests

Three new top-level regressions fail on the unmodified target main; the existing 32 top-level tests pass there:

  • Direct postprocessing: CJK and mixed-width code points are counted in runes; ASCII is unchanged.
  • Length split followed by filtering: valid rune-based sizes survive, with content, indices, and metadata retained.
  • Real Run entry point: CJK merge, filtering, oversized standalone chunks, and an ASCII control.

With the fix, on ZGC using Go 1.27.0:

  • CGO_ENABLED=0 go test ./internal/parser/chunk/ -count=1: all 35 top-level tests pass.
  • CGO_ENABLED=1 go test ./internal/parser/chunk/ -race -count=20: passes.
  • CGO_ENABLED=0 go vet ./internal/parser/chunk/: passes.
  • gofmt and git diff --check: pass.

The official bash build.sh --test ./internal/parser/chunk/ entry point remains blocked in the isolated test environment by the missing office_oxide native library. The touched package imports only the standard library and has no CGO source files, so the package-scoped checks above were run directly. Full-repository/native integration tests were not run.

Risk

Low and limited to the reported size of postprocessed non-ASCII chunks. The serialized field name and all chunking decisions remain unchanged.

@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6bc6910d-6bb1-4211-98df-82597f48c081

📥 Commits

Reviewing files that changed from the base of the PR and between 104c682 and b3e0e5c.


📒 Files selected for processing (2)
  • internal/parser/chunk/postprocess.go
  • internal/parser/chunk/postprocess_size_test.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.



📝 Walkthrough

Walkthrough

Postprocessing now records chunk sizes as Unicode code-point counts instead of UTF-8 byte lengths. Tests cover ASCII and Unicode content, length-split chunks, and Run behavior.

Changes

Chunk size calculation

Layer / File(s) Summary
Rune-based chunk sizing
internal/parser/chunk/postprocess.go, internal/parser/chunk/postprocess_size_test.go
PostprocessOperator.Execute sets each chunk’s Size to its rune count. Tests cover ASCII, CJK, supplementary-plane and combining characters, length-split chunks, and Run results. The merge-size logic is unchanged.

Priority: ⬇️ Low

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

Change: Bug fix


Merge Risk: ⚪ Minimal · up to b3e0e

No actionable merge-blocking risk is identified for this change.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: preserving postprocessed chunk sizes as rune counts.
Description check Passed The description includes the required Summary section and provides clear background, root cause, scope, tests, limitations, and risk information.
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.

  • Fix all pre-merge checks with AI
  • 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 counts each rune with care,
From ASCII to the characters rare.
Chunks keep their size in code points true,
Tests check the merged and filtered too.
Then off I hop, with numbers neat,
And Unicode dancing at my feet.

Comment @coderabbitai help to get the list of available commands.

@kokhlo kokhlo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally on b3e0e5c4 vs pristine main 5ccba271 (A/B with the same probe on both heads).

A/B probe (go test ./internal/parser/chunk/ -run TestProbeSizeAB -v, scenario → size on main → size on this PR):

scenario content main PR
merge CJK (MergeTargetSize: 8) 甲乙 丙丁 13 5
filter-only CJK 中文 6 2
astral-plane (CJK ext B) 𠈌𠈌𠈌 12 3
length split (window=2) 甲乙/丙丁/戊己 2/2/2 2/2/2 (unchanged, splitByLength already counts runes)

So the mismatch is exactly the one described in the body: merge/filter paths rewrote rune-based sizes into byte counts, 3x for CJK.

Mutation check: reverting just the RuneCountInString line (back to len(GetContent())) fails precisely the three new tests — TestPostprocessSizeCountsRunes, TestPostprocessPreservesLengthSplitRuneSizes, TestRunSizeCountsRunes — and nothing else in the package (go test ./internal/parser/chunk/ -count=1: ok before/after).

Consistency check: I grepped for other Size = len(...) writes near chunk paths — mergeChunks/filterChunks already count runes (len([]rune(...)) / bufRunes), splitByLength writes end-start over a rune slice, so this line was the only byte-based Size write left in the package. Downstream consumers of ChunkData.Size are the CLI chunk explain path (internal/cli/user_command.go:3258) and JSON serialization — both treat it as a display/metadata number, no byte-arithmetic on it anywhere, so the switch cannot break a consumer.

One non-blocking note: the JSON field is documented nowhere as bytes or runes; if some external consumer (API response for chunk listings?) assumed bytes, this is a behavior change — but I found no such consumer in-tree, and the length splitter (the only other Size producer) has always been rune-based, so runes is the consistent contract.

@JinHai-CN
JinHai-CN requested a review from xugangqiang October 13, 2026 03:25
@xugangqiang xugangqiang added the ci Continue Integration label Oct 13, 2026
@xugangqiang

Copy link
Copy Markdown
Contributor

LGTM

@xugangqiang xugangqiang added ci Continue Integration and removed ci Continue Integration labels Oct 14, 2026
@yuzhichang

Copy link
Copy Markdown
Member

Hold on. Lets unify the size of bytes/runes/tokens later.

@yuzhichang
yuzhichang marked this pull request as draft October 14, 2026 10:00

@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.

No blocking findings in the rune-count change itself; the size is now consistent with the splitter and merge/filter limits. Keeping this as a comment because the PR is still a draft and the earlier direction on unifying byte/rune/token sizing is still open.

@kassdinzheng123

Copy link
Copy Markdown
Contributor Author

Closing this draft PR since chunk-size unit unification is still on hold and #22317 already tracks the same rune-count fix as a non-draft PR.

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.

4 participants