Skip to content

Fix naive chunking for windows - #16767

Merged
yingfeng merged 1 commit into
infiniflow:mainfrom
yingfeng:fixchunk
Jul 9, 2026
Merged

yingfeng merged 1 commit into
infiniflow:mainfrom
yingfeng:fixchunk

Conversation

@yingfeng

@yingfeng yingfeng commented Jul 9, 2026

Copy link
Copy Markdown
Member

\r is ignored for splitting boundaries

@yingfeng yingfeng added the ci Continue Integration label Jul 9, 2026
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jul 9, 2026
@yingfeng
yingfeng marked this pull request as draft July 9, 2026 03:47
@dosubot dosubot Bot added the 🐞 bug Something isn't working, pull request that fix bug. label Jul 9, 2026
@yingfeng
yingfeng marked this pull request as ready for review July 9, 2026 03:47
@coderabbitai

coderabbitai Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The naive_merge function in rag/nlp/__init__.py now normalizes newline characters in input section tuples, converting \r\n and standalone \r sequences to \n before subsequent delimiter-based splitting and merging logic.

Changes

Newline Normalization

Layer / File(s) Summary
Normalize newlines before merging
rag/nlp/__init__.py
Adds a preprocessing step converting \r\n and \r to \n in each section tuple before merge/split logic runs.

Estimated code review effort: 1 (Trivial) | ~3 minutes

Poem

A rabbit hops through lines of text,
Smoothing carriage returns, no more perplexed,
\r\n, \r, all become \n,
Splitting sections cleanly, one by one. 🐰

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is too brief and does not follow the required Summary template or provide background context. Add a ### Summary section that briefly explains the issue, the Windows newline behavior, and why the change is needed.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Title check ✅ Passed The title matches the main change: fixing naive chunking on Windows by normalizing newline behavior.

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 `@rag/nlp/__init__.py`:
- Around line 1165-1166: `naive_merge_with_images` still splits the original
text without normalizing line endings, so Windows-style inputs can produce
different chunk boundaries than `naive_merge`. Update the
`naive_merge_with_images` path to apply the same `\r\n` and `\r` normalization
used in `naive_merge` before any delimiter splitting, and keep the behavior
consistent with the existing `sections` normalization logic.
🪄 Autofix (Beta)

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

Run ID: df8bcb40-daad-449f-a52b-88561a18c626

📥 Commits

Reviewing files that changed from the base of the PR and between ae96e63 and 1f925e5.

📒 Files selected for processing (1)
  • rag/nlp/__init__.py

Comment thread rag/nlp/__init__.py
Comment on lines +1165 to +1166
# Normalize line endings so delimiter ``\n`` matches ``\r\n`` and standalone ``\r``.
sections = [(s.replace("\r\n", "\n").replace("\r", "\n"), pos) for s, pos in sections]

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.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check if naive_merge_with_images has its own splitting logic or calls naive_merge
rg -n "def naive_merge_with_images" rag/nlp/__init__.py
# Show the function body to inspect for newline handling
ast-grep outline rag/nlp/__init__.py --match naive_merge_with_images --view expanded

Repository: infiniflow/ragflow

Length of output: 401


🏁 Script executed:

#!/bin/bash
sed -n '1230,1325p' rag/nlp/__init__.py

Repository: infiniflow/ragflow

Length of output: 4568


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== callers of naive_merge_with_images =="
rg -n "naive_merge_with_images\s*\(" -S rag . || true

echo
echo "== nearby text normalization around naive_merge/naive_merge_with_images =="
sed -n '1080,1325p' rag/nlp/__init__.py | rg -n "normalize line endings|replace\\(\"\\\\r\\\\n\"|replace\\(\"\\\\r\"|naive_merge|naive_merge_with_images|get_delimiters|re.split|text_str|text_pos"

echo
echo "== definitions of get_delimiters and naive_merge =="
rg -n "def get_delimiters|def naive_merge\\(" rag/nlp/__init__.py
sed -n '1010,1185p' rag/nlp/__init__.py

Repository: infiniflow/ragflow

Length of output: 9457


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== caller context =="
sed -n '1190,1255p' rag/app/naive.py

echo
echo "== behavior probe for CRLF/CR on the current split path =="
python3 - <<'PY'
import re

delimiter = r"\n。;!?"
dels = delimiter

def split_current(text):
    pieces = []
    for sub_sec in re.split(r"(%s)" % dels, text, flags=re.DOTALL):
        if not sub_sec or re.fullmatch(dels, sub_sec):
            continue
        pieces.append(sub_sec)
    return pieces

samples = {
    "lf": "a\nb",
    "crlf": "a\r\nb",
    "cr": "a\rb",
}

for name, s in samples.items():
    print(name, split_current(s))
PY

Repository: infiniflow/ragflow

Length of output: 3468


Normalize line endings in naive_merge_with_images. rag/nlp/__init__.py:1230 still splits raw text directly and never applies the \r\n/\r normalization used by naive_merge, so Windows files can chunk differently on this path.

🤖 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 `@rag/nlp/__init__.py` around lines 1165 - 1166, `naive_merge_with_images`
still splits the original text without normalizing line endings, so
Windows-style inputs can produce different chunk boundaries than `naive_merge`.
Update the `naive_merge_with_images` path to apply the same `\r\n` and `\r`
normalization used in `naive_merge` before any delimiter splitting, and keep the
behavior consistent with the existing `sections` normalization logic.

@yingfeng
yingfeng merged commit b9ed077 into infiniflow:main Jul 9, 2026
9 of 15 checks passed
@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (40cb581) to head (1f925e5).
⚠️ Report is 23 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #16767   +/-   ##
=======================================
  Coverage   93.16%   93.16%           
=======================================
  Files          10       10           
  Lines         717      717           
  Branches      118      118           
=======================================
  Hits          668      668           
  Misses         29       29           
  Partials       20       20           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. 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.

1 participant