Skip to content

test(chunker): lock non-text segments as standalone on merge (closes #17889) - #17896

Merged
yuzhichang merged 1 commit into
infiniflow:mainfrom
xugangqiang:test/chunker-17889-nontext-standalone
Aug 6, 2026
Merged

yuzhichang merged 1 commit into
infiniflow:mainfrom
xugangqiang:test/chunker-17889-nontext-standalone

Conversation

@xugangqiang

Copy link
Copy Markdown
Contributor

Background

Issue #17889 asks that, when merging adjacent segments, the chunker first
checks each segment's type and only merges text segments — table,
image, and any other non-text type must each remain a standalone chunk
and must never be merged with a neighbouring segment.

Why this PR closes #17889 (no Go code change required)

After tracing the Go TokenChunker, the requirement is already satisfied
on the structured (JSON / chunks) path. The type-aware rule is enforced at
three layers in internal/ingestion/component/chunker/:

  • common.go:138 itemDocType derives the type from doc_type_kwd
    ("table" -> "table", "image" -> "image", anything else -> "text").
    It does not depend on the ck_type field being populated, so the type
    survives even when only doc_type_kwd is set (e.g. upstream
    Title/Group/Hierarchy chunks).
  • token.go:756 chunkFromItem emits a non-text item as a single standalone
    chunk before the merge loop ever runs.
  • token.go:1050 mergeByTokenSizeFromJSON forces any non-text chunk
    standalone (if ck.CKType != "text"); and token.go:991 starts a fresh
    text chunk after a non-text chunk, so text on either side of a
    table/image is never merged across it.

The only path without type information is the raw markdown/text/html string
path (PayloadFormatMarkdown/Text/HTML), where the input is by contract an
untyped string and applyChildrenDelim hard-codes CKType: "text" so
merging is correct. There is no non-text segment to merge there, so this is
out of #17889's scope (which is about the merge logic).

Why the Python side is deferred

The Python naive parser path does not thread a ck_type through to
merge_paragraphs / naive_merge / naive_merge_with_images
(rag/nlp/__init__.py): its parsers emit flat (text, pos) sections plus a
parallel section_images list, and the type-aware _merge_cks rule
(rag/nlp/__init__.py:1749) is only wired into the docx path. Propagating
ck_type end-to-end across every Python parser is a large refactor, so it is
intentionally not part of this PR. The Go engine is the active ingestion
path, and it already honors the rule.

This PR

Adds a regression-lock (characterization) test, not a fix:

  • TestTokenChunker_InvokeJSONPayload_KeepsNonTextStandalone feeds a
    [text, table, text, image, text] structured payload and asserts it
    produces exactly five standalone chunks in the order
    text, table, text, image, text — proving tables/images stay standalone
    and text on either side is not merged across them.

Verified green:

bash build.sh --test -run TestTokenChunker_InvokeJSONPayload_KeepsNonTextStandalone ./internal/ingestion/component/chunker/...
--- PASS: TestTokenChunker_InvokeJSONPayload_KeepsNonTextStandalone (0.07s)

Related

… for infiniflow#17889)

Add TestTokenChunker_InvokeJSONPayload_KeepsNonTextStandalone asserting that
a [text, table, text, image, text] JSON payload yields five standalone chunks
with the correct doc_type_kwd order. The Go merge already enforces this via
itemDocType (common.go:138), chunkFromItem (token.go:756) and
mergeByTokenSizeFromJSON (token.go:1050, token.go:991).

Closes infiniflow#17889.
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Aug 5, 2026
@coderabbitai

coderabbitai Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This pull request adds one regression test for TokenChunker JSON payload handling. The test covers a text/table/text/image/text sequence. It verifies five ordered chunks, preserved document types, and no text merge across non-text items.

Changes

JSON payload chunking regression

Layer / File(s) Summary
Non-text standalone chunk regression
internal/ingestion/component/chunker/token_test.go
Adds TestTokenChunker_InvokeJSONPayload_KeepsNonTextStandalone. The test uses a JSON payload with text, table, and image items. It asserts five standalone chunks, preserved type/order, and separated text around non-text items.

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

Possibly related PRs

Suggested labels: 🧪 test

Suggested reviewers: yuzhichang

Poem

Bunny taps keys with careful sight,
guarding chunks through day and night.
Table stays still, image too,
text keeps its place all the way through.
🐇 Five neat hops in ordered line.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The test covers the Go JSON path, but the linked issue requires type-aware merging in naive paths and direct table+image coverage, which this PR does not provide. Implement type-aware merging in naive_merge, naive_merge_with_images, or merge_paragraphs, and add tests for text+table, text+image, and table+image cases.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the added chunker regression test and its non-text standalone behavior.
Description check ✅ Passed The description explains the background, scope, test behavior, deferred Python work, and verification, but it does not use the required Summary heading.
Out of Scope Changes check ✅ Passed The PR adds only a regression test for the linked chunking behavior and contains no unrelated code or documentation changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

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

Inline comments:
In `@internal/ingestion/component/chunker/token_test.go`:
- Around line 252-259: Update the token chunking test around the existing item
sequence in token_test.go to cover directly adjacent non-text items by adding a
table/image pair with no text between them, or create a focused dedicated test
for that case. Use the existing chunking assertions to verify the two items stay
in document order and remain standalone chunks with their respective table and
image types, while keeping the current text/non-text coverage intact.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 14ed45c5-3d94-4ddb-9594-21a63e82d2c7

📥 Commits

Reviewing files that changed from the base of the PR and between d9d7569 and 0b9f91b.

📒 Files selected for processing (1)
  • internal/ingestion/component/chunker/token_test.go

Comment on lines +252 to +259
// text, table, text, image, text — in document order.
items := []map[string]any{
{"text": "Alpha section text content", "doc_type_kwd": "text"},
{"text": "<table>caption</table>", "doc_type_kwd": "table"},
{"text": "Beta section text content", "doc_type_kwd": "text"},
{"text": "[image]", "doc_type_kwd": "image"},
{"text": "Gamma section text content", "doc_type_kwd": "text"},
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Add an adjacent table and image case.

The current sequence separates table and image with a text item. It verifies text/non-text boundaries, but it cannot detect a merge of directly adjacent non-text items. Add an adjacent table/image pair, or add a dedicated test, and assert their order and standalone chunk types. The stated issue objective requires this case.

🤖 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/chunker/token_test.go` around lines 252 - 259,
Update the token chunking test around the existing item sequence in
token_test.go to cover directly adjacent non-text items by adding a table/image
pair with no text between them, or create a focused dedicated test for that
case. Use the existing chunking assertions to verify the two items stay in
document order and remain standalone chunks with their respective table and
image types, while keeping the current text/non-text coverage intact.

@xugangqiang xugangqiang added the ci Continue Integration label Aug 5, 2026
@yuzhichang

Copy link
Copy Markdown
Member

LGTM

@yuzhichang
yuzhichang merged commit e95c813 into infiniflow:main Aug 6, 2026
6 checks passed
@xugangqiang
xugangqiang deleted the test/chunker-17889-nontext-standalone branch August 7, 2026 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Chunking] Only merge text segments; keep table/image as standalone chunks (follow-up to #17808)

2 participants