Skip to content

fix(chunk): number paragraph chunks by their position - #21823

Merged
yuzhichang merged 2 commits into
infiniflow:mainfrom
Steve-20-ai:fix/chunk-paragraph-index
Nov 2, 2026
Merged

yuzhichang merged 2 commits into
infiniflow:mainfrom
Steve-20-ai:fix/chunk-paragraph-index

Conversation

@Steve-20-ai

Copy link
Copy Markdown
Contributor

What happens

splitByParagraph numbers each chunk with the paragraph's position in strings.Split(text, "\n"):

for i, p := range paragraphs {
    trimmed := strings.TrimSpace(p)
    if trimmed == "" {
        continue
    }
    chunks = append(chunks, ChunkData{
        Content: trimmed,
        Index:   i,
        ...

Blank lines are skipped, so the index keeps the holes they left. For

第一段
<blank>
<blank>
第二段

Run(text, ChunkOptions{SplitStrategy: "paragraph"}) returns two chunks indexed 0 and 3 — and Run hands SplitChunks straight to the caller when no postprocess stage is configured (execute.go), so that is what the chunk CLI command prints:

Chunk index: 0
Chunk index: 3

The number is also unstable across configurations: with MergeTargetSize or FilterMinLength set, PostprocessOperator.Execute re-numbers every chunk (chunks[i].Index = i), so the same input reports 0, 1 in one case and 0, 3 in the other.

Every other strategy builds its index from the chunk's own position (Index: len(chunks) in splitSentences, splitByChar and splitByLength), and split_test.go asserts exactly that for the length strategy.

Change

splitByParagraph numbers chunks by their position in the output, matching the other strategies and the postprocess re-index.

Tests

New internal/parser/chunk/split_by_paragraph_test.go:

  • TestSplitByParagraphSkipsBlankLines — blank and whitespace-only lines are dropped (the strategy had no test at all).
  • TestSplitByParagraphIndexMatchesPosition — fails on main with chunk 1 ("第二段") Index = 3, want 1.
  • TestSplitByParagraphBlankInput — empty and whitespace-only input produce no chunks.

go test ./internal/parser/chunk passes (unit tier, no external services) and go vet ./internal/parser/chunk is clean.

AI assistance

This change was prepared with AI assistance: the defect was reproduced locally, the fix written, and the tests run against this tree. The change was reviewed before submission.

@coderabbitai

coderabbitai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

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: d663efbd-c824-4233-9c39-f893dc9c731e
Commits

Reviewing files that changed from the base of the PR and between 12d9a38 and 0d8536f.

Files selected for processing: 2
  • internal/parser/chunk/split.go
  • internal/parser/chunk/split_by_paragraph_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

The paragraph split strategy now assigns consecutive indices to emitted chunks. Blank and whitespace-only paragraphs do not create index gaps. Tests cover skipped lines, output indices, and empty or whitespace-only input.

Changes: Paragraph splitting

Layer / File(s) Summary
Consecutive chunk indices
internal/parser/chunk/split.go, internal/parser/chunk/split_by_paragraph_test.go
The splitter assigns each emitted chunk an index based on its position in the output. Tests check blank-line filtering, consecutive indices, and empty or whitespace-only input.

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

Change: Bug fix

Suggested reviewers: xugangqiang

Merge Risk: Minimal · up to 0d853

The paragraph indexing fix is ready to merge with no identified blocking risk.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: paragraph chunks are numbered by their output position.
Description check Passed The description explains the defect, the behavior change, the affected code path, the tests, and validation results. It does not use the template's "### Summary" heading, but it provides the required …
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.
Full details: Description check

Explanation

The description explains the defect, the behavior change, the affected code path, the tests, and validation results. It does not use the template's "### Summary" heading, but it provides the required summary content in the "### What happens" and "### Change" sections.

Full details: Docstring Coverage

Explanation

Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 4 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • 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 each chunk in line
Blank spaces leave no gaps behind
The indices hop in order true
Three tests watch the outputs through
Then carrots mark the review done

@Harsh23Kashyap Harsh23Kashyap left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verified in-tree at 0d8536f and approving.

Red/green both reproduce cleanly here. With just the splitByParagraph hunk reverted, TestSplitByParagraphIndexMatchesPosition fails: the three chunks of the test document come back indexed 0, 3, 5 through the blank lines, exactly the holes described. At this head the full chunk package suite passes, including the three new tests. Two runs each way, identical results.

The instability claim checks out as well: PostprocessOperator.Execute re-numbers every chunk, so with a merge or filter stage configured the same document reports 0, 1, 2, and without one it reported 0, 3, 5. Nothing outside the package consumes SplitChunks.Index, and the merge bookkeeping in postprocess only uses it as a boundary marker, so numbering by output position is safe here.

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

Reviewed merge-base..head (2 commits, 2 files).

Change: splitByParagraph replaced the paragraph's slice position with len(chunks) so the emitted Index values are dense and 0-based, matching splitSentences, splitByChar and splitByLength. Blank lines are already skipped by the trimmed == "" guard, so gaps were guaranteed on any text containing an empty line.

Call-chain check

  • ChunkData.Index consumers inside the package: PostprocessOperator.Execute rewrites Index unconditionally (internal/parser/chunk/postprocess.go:96), and mergeChunks only uses it as a provisional carry-over before that rewrite. Both are unaffected by the change, and the postprocess path now receives a correct input sequence instead of relying on the rewrite to paper over gaps.
  • Run() exposes SplitChunks as ResultChunks when no postprocess stage is configured (internal/parser/chunk/execute.go:90); that path was the one leaking 0,3-style holes, and it is now dense.
  • The only production caller of chunk.Run is the CLI chunk command (internal/cli/user_command.go:3324), which prints Index verbatim. The ingestion pipeline uses internal/ingestion/component/chunker, not this splitter, so no persisted chunk_index/chunk-id identity shifts.
  • No map or slice is keyed by ChunkData.Index anywhere in the tree, so no dedupe or position-mapping path depends on the old sparseness.

Tests: the new file pins the three behaviors that matter — blank-line skipping, dense positional index, and empty/whitespace-only input returning zero chunks — and matches the existing runLength helper style in split_test.go.

No blocking findings.

@yuzhichang
yuzhichang merged commit 673da1e into infiniflow:main Nov 2, 2026
6 checks passed
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.

5 participants