RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(chunker): honour Python {,n} level patterns and keep long numbered paragraphs as body (#21641)
CCLUCKY5af649b3f301
diff --git a/internal/ingestion/component/chunker/title.go b/internal/ingestion/component/chunker/title.goindex 241e8e0f4e..83d7538000 100644--- a/internal/ingestion/component/chunker/title.go+++ b/internal/ingestion/component/chunker/title.go@@ -53,6 +53,13 @@// the layout-title fallback. It never overrides an existing level// assignment.//+// - Two Go-side guards narrow the regex branch: level patterns are+// compiled with Python's `{,n}` repetition rewritten to RE2's+// `{0,n}` (the shipped templates use it, and RE2 reads the braces+// literally), and a matched line that is too long to be a heading+// stays body text so include_heading_content cannot copy a body+// paragraph into every chunk below it (#21585).+//// - GROUP-TITLE and HIERARCHY-TITLE are separate Go files// (`group.go`, `hierarchy.go`); they share the resolve_levels// path with TitleChunker but their build_chunks logic differs.@@ -150,7 +157,8 @@ func selectLevelGroup(lines []string, rawLevels [][]string) []string {}// Mirror common.py:select_level_group — a bullet-looking// line cannot count as a heading hit for any family.- if notBullet(text) {+ // A long body paragraph does not count either.+ if notHeadingCandidate(text) {continue}for _, pattern := range group {@@ -192,7 +200,7 @@ func matchRegexLevel(text string, group []string) int {}// Mirror common.py:match_regex_level — a bullet-looking line is not// a heading regardless of the regex family.- if notBullet(stripped) {+ if notHeadingCandidate(stripped) {return 0}for lvl, pattern := range group {@@ -226,6 +234,42 @@ func notBullet(s string) bool {return false}+// notHeadingCandidate reports whether a line matched by a level pattern must+// still not be promoted to a heading.+func notHeadingCandidate(s string) bool {+ return notBullet(s) || headingTooLong(s)+}++// numberedMarkerRe matches a numbering prefix followed by whitespace: "(1) ",+// "(一)", "1. ", "一、", "第三章 ". The whitespace after such a marker is+// structural, not word separation, so headingTooLong must look past it.+var numberedMarkerRe = regexp.MustCompile(`^(?:[\((][0-9零一二三四五六七八九十百]{1,3}[\))]|[0-9]{1,3}[\.、.\))]|[零一二三四五六七八九十百]{1,4}[、..]|第[零一二三四五六七八九十百0-9]+[章节])[ \t\x{3000}]+`)++// headingTooLong reports whether a line is too long to be a heading. A numbered+// body paragraph that happens to start like a heading — "(1)社区口腔健康教育:+// 是在社区范围内…" of ~300 runes — is not a heading: with+// include_heading_content its text would be prepended to every chunk below it+// (#21585). Only notTitle's length test applies here. Its punctuation test is+// skipped because numbering families legitimately match headings carrying a+// comma or colon ("Chapter 3: Methods, Results and Evaluation"), and an+// explicit Markdown ATX marker is structural at any length.+func headingTooLong(s string) bool {+ if strings.HasPrefix(s, "#") || notTitleException.MatchString(s) {+ return false+ }+ if len(strings.Fields(s)) > 12 {+ return true+ }+ if !strings.Contains(s, " ") && utf8.RuneCountInString(s) >= 32 {+ return true+ }+ // The marker delimiter is often the only space on a numbered CJK paragraph,+ // which hid it from the rune cap above. Measure the text after the marker;+ // a multi-word heading keeps its spaces and stays a heading.+ rest := numberedMarkerRe.ReplaceAllString(s, "")+ return !strings.Contains(rest, " ") && !strings.Contains(rest, "\u3000") && utf8.RuneCountInString(rest) >= 32+}+// notTitlePatterns mirrors rag/nlp.not_title:// - notTitleException: `第...条` is always a title (never "not a title").// - notTitlePunct: a body line typically carries one of these punctuation@@ -371,7 +415,7 @@ func resolveTitleLevels(records []lineRecord, p *titleChunkerParam) []int {trimmed := strings.TrimSpace(rec.text)matched := falsefor j, pat := range bulletPatterns[bull] {- if pat.MatchString(trimmed) && !notBullet(trimmed) {+ if pat.MatchString(trimmed) && !notHeadingCandidate(trimmed) {out[i] = j + 1matched = truebreak@@ -662,6 +706,13 @@ func trim(s string) string {return strings.TrimSpace(s)}+// pythonRepetitionRe rewrites Python's `{,n}` repetition to RE2's `{0,n}`.+// Python's re reads `{,2}` as "zero to two"; RE2 reads the braces literally,+// so a pattern copied from a Python config compiled but never matched. The+// Book, Paper, Laws and Manual templates all ship `[\((][0-9]{,2}[\))]`,+// whose `(1)` level was silently dead.+var pythonRepetitionRe = regexp.MustCompile(`\{,(\d+)\}`)+// compileLevelPattern returns a compiled regex for `pattern`. Returns// nil on empty/error to skip the entry — same effect as a regex// mismatch in python (the row falls through to body).@@ -674,6 +725,7 @@ func compileLevelPattern(pattern string) *regexp.Regexp {if pattern == "" {return nil}+ pattern = pythonRepetitionRe.ReplaceAllString(pattern, "{0,$1}")anchored := patternif !strings.HasPrefix(pattern, "^") {anchored = "^(?:" + pattern + ")"diff --git a/internal/ingestion/component/chunker/title_numbered_heading_test.go b/internal/ingestion/component/chunker/title_numbered_heading_test.gonew file mode 100644index 0000000000..636fd7ba6b--- /dev/null+++ b/internal/ingestion/component/chunker/title_numbered_heading_test.go@@ -0,0 +1,200 @@+//+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.+//++package chunker++import (+ "strings"+ "testing"++ "ragflow/internal/ingestion/component/schema"+)++// numberedHeadingLevels mirrors the last level family shipped in the Book,+// Paper, Laws and Manual templates. The final pattern uses Python's `{,n}`+// repetition, which Go's regexp does not accept.+var numberedHeadingLevels = [][]string{{+ `第[零一二三四五六七八九十百0-9]+章`,+ `第[零一二三四五六七八九十百0-9]+节`,+ `[零一二三四五六七八九十百]+[ 、]`,+ `[\((][零一二三四五六七八九十百]+[\))]`,+ `[\((][0-9]{,2}[\))]`,+}}++func numberedHeadingParam() *titleChunkerParam {+ p := &titleChunkerParam{}+ p.Update(map[string]any{"levels": numberedHeadingLevels, "hierarchy": 5})+ return p+}++// TestCompileLevelPatternAcceptsPythonRepetition pins the dialect fix: Python's+// re reads `{,n}` as `{0,n}`, Go's RE2 reads the braces literally, so a pattern+// copied from a Python config compiled but never matched.+func TestCompileLevelPatternAcceptsPythonRepetition(t *testing.T) {+ re := compileLevelPattern(`[\((][0-9]{,2}[\))]`)+ if re == nil {+ t.Fatal("compileLevelPattern returned nil")+ }+ for _, line := range []string{"(1) 概述", "(12) 方法"} {+ if !re.MatchString(line) {+ t.Errorf("pattern did not match %q; `{,n}` was treated literally", line)+ }+ }+ if re.MatchString("(123) 超出两位") {+ t.Error("pattern matched a three-digit marker; `{,2}` must bound the repetition")+ }+}++// TestResolveTitleLevels_NumberedHeadingNeedsShortText pins the second half of+// the fix. Enabling the `(1)` family makes every numbered paragraph a heading+// candidate, which is what #21585 reports: a ~300-rune numbered paragraph is+// body text, so it must stay at body level while a short numbered heading is+// promoted.+func TestResolveTitleLevels_NumberedHeadingNeedsShortText(t *testing.T) {+ long := "(1)社区口腔健康教育:是在社区范围内,以增进居民口腔健康为目标,通过有组织的社会活动,普及口腔健康知识," ++ "提高居民自我口腔保健意识和能力,改善口腔健康状况的综合性健康教育活动,内容包括口腔健康检查、口腔卫生指导、" ++ "窝沟封闭、局部用氟、早期龋齿充填等适宜技术,并对重点人群实施分类管理。"+ records := []lineRecord{+ {text: "(三) 实施口腔卫生服务", docType: "text"},+ {text: "(1) 健康教育与健康促进", docType: "text"},+ {text: long, docType: "text"},+ }+ levels := resolveTitleLevels(records, numberedHeadingParam())+ if levels[0] == bodyLevel {+ t.Errorf("short numbered heading %q was not promoted", records[0].text)+ }+ if levels[1] == bodyLevel {+ t.Errorf("short numbered heading %q was not promoted", records[1].text)+ }+ if levels[2] != bodyLevel {+ t.Errorf("long numbered paragraph (level %d) must stay body text, not become a heading", levels[2])+ }+}++// TestHierarchyTitleChunker_LongNumberedParagraphNotRepeated pins the user+// visible outcome from #21585: with include_heading_content the heading text is+// prepended to every chunk under it, so a body paragraph promoted to a heading+// is copied into several chunks.+func TestHierarchyTitleChunker_LongNumberedParagraphNotRepeated(t *testing.T) {+ long := "(1)社区口腔健康教育:是在社区范围内,以增进居民口腔健康为目标,通过有组织的社会活动,普及口腔健康知识," ++ "提高居民自我口腔保健意识和能力,改善口腔健康状况的综合性健康教育活动,内容包括口腔健康检查、口腔卫生指导、" ++ "窝沟封闭、局部用氟、早期龋齿充填等适宜技术,并对重点人群实施分类管理。"+ var b strings.Builder+ b.WriteString("(三) 实施口腔卫生服务\n")+ b.WriteString("基层医疗卫生机构应当组织开展口腔健康教育。\n")+ b.WriteString(long + "\n")+ for i := 0; i < 8; i++ {+ b.WriteString("后续段落" + strings.Repeat("内容", 60) + ",补充说明。\n")+ }++ c, err := NewHierarchyTitleChunker(map[string]any{+ "hierarchy": 5,+ "include_heading_content": true,+ "levels": numberedHeadingLevels,+ })+ if err != nil {+ t.Fatalf("NewHierarchyTitleChunker: %v", err)+ }+ out, err := c.Invoke(t.Context(), nil, map[string]any{"name": "doc.txt", "text": b.String()})+ if err != nil {+ t.Fatalf("Invoke: %v", err)+ }+ chunks, ok := schema.ChunkCarrierToMaps(out["chunks"])+ if !ok {+ t.Fatal("ChunkCarrierToMaps failed")+ }+ appearances := 0+ for _, ck := range chunks {+ text, _ := ck["text"].(string)+ appearances += strings.Count(text, long)+ }+ if appearances != 1 {+ t.Fatalf("long numbered paragraph appears in %d chunk texts, want exactly 1", appearances)+ }+}++// TestResolveTitleLevels_LongNumberedParagraphWithSpaceStaysBody: the marker+// delimiter is often the line's only space, so a space must not exempt a long+// numbered paragraph from the length guard.+func TestResolveTitleLevels_LongNumberedParagraphWithSpaceStaysBody(t *testing.T) {+ long := "(1) 社区口腔健康教育:是在社区范围内,以增进居民口腔健康为目标,通过有组织的社会活动,普及口腔健康知识," ++ "提高居民自我口腔保健意识和能力,改善口腔健康状况的综合性健康教育活动,内容包括口腔健康检查、口腔卫生指导、" ++ "窝沟封闭、局部用氟、早期龋齿充填等适宜技术,并对重点人群实施分类管理。"+ records := []lineRecord{+ {text: "(1) 健康教育与健康促进", docType: "text"},+ {text: long, docType: "text"},+ }+ levels := resolveTitleLevels(records, numberedHeadingParam())+ if levels[0] == bodyLevel {+ t.Errorf("short numbered heading %q was not promoted", records[0].text)+ }+ if levels[1] != bodyLevel {+ t.Errorf("long numbered paragraph with a space after the marker (level %d) must stay body text", levels[1])+ }+}++// TestResolveTitleLevels_BulletFallbackSkipsLongNumberedParagraph: when no+// regex family wins, the Chunker-1.7 bullet fallback promotes numbered lines.+// It must apply the same length guard, otherwise a document with no short+// heading turns every long numbered paragraph into a heading.+func TestResolveTitleLevels_BulletFallbackSkipsLongNumberedParagraph(t *testing.T) {+ long := "(1) 社区口腔健康教育:是在社区范围内,以增进居民口腔健康为目标,通过有组织的社会活动,普及口腔健康知识," ++ "提高居民自我口腔保健意识和能力,改善口腔健康状况的综合性健康教育活动。"+ records := []lineRecord{+ {text: "(2) 概述", docType: "text"},+ {text: long, docType: "text"},+ }+ p := &titleChunkerParam{}+ p.Update(map[string]any{"hierarchy": 3})+ levels := resolveTitleLevels(records, p)+ if levels[0] == bodyLevel {+ t.Errorf("short bullet %q was not promoted by the bullet fallback", records[0].text)+ }+ if levels[1] != bodyLevel {+ t.Errorf("long numbered paragraph (level %d) must stay body text in the bullet fallback", levels[1])+ }+}++// TestResolveTitleLevels_MultiWordNumberedHeadingStaysHeading: the space fix is+// scoped to space-free CJK runs, so a long multi-word numbered heading keeps+// its level.+func TestResolveTitleLevels_MultiWordNumberedHeadingStaysHeading(t *testing.T) {+ records := []lineRecord{+ {text: "(1) Overview of the System Architecture and Its Components", docType: "text"},+ }+ levels := resolveTitleLevels(records, numberedHeadingParam())+ if levels[0] == bodyLevel {+ t.Error("long multi-word numbered heading was demoted to body text")+ }+}++// TestResolveTitleLevels_LongMarkdownHeadingStaysHeading: an explicit ATX+// marker is structural, so the length guard must not demote it.+func TestResolveTitleLevels_LongMarkdownHeadingStaysHeading(t *testing.T) {+ p := &titleChunkerParam{}+ p.Update(map[string]any{"levels": [][]string{{`^#[^#]`, `^##[^#]`}}})+ records := []lineRecord{+ {text: "# A Comprehensive Survey of Methods for Efficient Retrieval Augmented Generation in Production Systems", docType: "text"},+ {text: "body text", docType: "text"},+ }+ levels := resolveTitleLevels(records, p)+ if levels[0] == bodyLevel {+ t.Error("long markdown heading was demoted to body text")+ }+ if levels[1] != bodyLevel {+ t.Errorf("body line level = %d, want body", levels[1])+ }+}