Skip to content

fix(parser): keep short-line body pages out of PDF TOC removal - #20988

Merged
yuzhichang merged 2 commits into
infiniflow:mainfrom
jay77721:fix/pdf-toc-shortline-body-guard
Oct 15, 2026
Merged

yuzhichang merged 2 commits into
infiniflow:mainfrom
jay77721:fix/pdf-toc-shortline-body-guard

Conversation

@jay77721

Copy link
Copy Markdown
Member

Summary

RemoveTOCBoxes (the PDF remove_toc path) deletes whole pages it reads as
table-of-contents pages, and its only content guard — carriesProse() — counts
boxes longer than 60 runes. A corpus of short lines never produces such boxes:
in 民法典 every article line wraps at ~35 runes, so prose stayed 0 on
every page
of the document (measured), and the guard never fired.

Consequence on a real file — page 7 of 民法典 carries the TOC tail and
the body opening. It satisfied the TOC shape (markers=6, short boxes=24) and
passed the guard (prose=0), so the whole page was deleted: the
第一章 基本规定 heading and articles 第一条–第八条 were silently lost
from the parse, which then corrupted the downstream chunk hierarchy (chapter-1
articles appearing under the wrong heading).

Related Issue

Related to the box-level TOC detector introduced by #19779 (where the guard
lives) and adjacent to the CJK TOC detection issue #19744. Neither is closed by
this PR.

Changes

  • pageShape gains a body counter: boxes that are neither chapter markers
    nor page numbers, longer than tocBodyRunes (20), and not leader-only runs
    (tocLeaderOnlyPattern — the dot column of a dotted TOC entry carries no
    body text and must not count).
  • carriesProse() becomes carriesLongProse() || body > tocMaxBodyBoxes (2):
    a page carrying several such boxes is a body page that no signal may drop.
  • The leading-run scan keeps using carriesLongProse() alone, so a
    cover/copyright page (short metadata lines, no body text) does not stop the
    scan before the TOC is reached; every drop still passes the full guard.
  • New regression test TestRemoveTOCBoxes_ShortLineBodyPageIsNotTOC
    reproducing the mixed TOC-tail + short-article page; verified to fail without
    this change.

Test Plan

  • bash build.sh --test ./internal/deepdoc/parser/pdf/layout/ — green,
    including the new test (red without the fix).
  • Real-document A/B (remove_toc=true, full-output content hash with vs
    without the fix):
    • 民法典_前15页.pdf: only the intended change — the wrongly dropped page is
      restored (114 → 127 items, additions only; 第一章 + 第一条–第八条 back);
    • 中华人民共和国法典_前15页.pdf and an English paper: byte-identical
      output;
    • 道德经 (TOC + chapters): TOC pages still dropped, body kept; the only delta
      is the last pure-TOC page retained — the safe direction (a missed TOC page
      costs noise chunks; a dropped content page loses text).
  • Content spot checks on 道德经_目录加前两章.pdf: 前言 / 第1章 / 第2章 /
    道可道 all present.

RemoveTOCBoxes classifies a page as TOC from short boxes with chapter markers, guarded only by carriesProse (>2 boxes longer than 60 runes). A corpus of short lines (a legal code's articles wrap at ~35 runes, verse, lists) never produces such boxes, so prose stays 0 and a mixed page (TOC tail + body start) is read as a TOC page and dropped whole — losing body text such as an entire chapter plus its articles.

Add a second body-evidence arm: marker-less, leader-less boxes longer than 20 runes. The leading-run scan keeps using the long-box signal alone so a cover page (short metadata lines, no body text) does not stop the scan before the TOC; every drop still passes the full guard.
@jay77721 jay77721 added bug go Pull requests that update go code parsing size:M This PR changes 30-99 lines, ignoring generated files. ci Continue Integration labels Oct 15, 2026
@coderabbitai

coderabbitai Bot commented Oct 15, 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: 7d59d374-cb88-42d1-8403-7a0eeef24ba1
📥 Commits

Reviewing files that changed from the base of the PR and between e449dda and 34fdd02.

📒 Files selected for processing: 2
  • internal/deepdoc/parser/pdf/layout/toc_header_footer.go
  • internal/deepdoc/parser/pdf/layout/toc_header_footer_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

📝 Walkthrough

TOC page detection now counts qualifying short text boxes as body evidence. The leading-page scan stops on long prose, while the final removal guard also checks for multiple qualifying body boxes. A regression test checks that a mixed TOC and body-text page remains intact.

Changes: TOC Body-Text Detection

Layer / File(s) Summary
Count short-line body evidence
internal/deepdoc/parser/pdf/layout/toc_header_footer.go
Page-shape classification counts marker-less, non-leader boxes longer than 20 runes. Leader-only boxes do not count as body evidence.
Apply TOC guards and test mixed pages
internal/deepdoc/parser/pdf/layout/toc_header_footer.go, internal/deepdoc/parser/pdf/layout/toc_header_footer_test.go
The leading-run scan stops on long prose. The final removal guard also protects pages with more than two qualifying body boxes. A regression test verifies that all 14 boxes on a mixed page remain.

Priority: ➖ Normal

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

Change: Bug fix

Suggested reviewers: flyingwhit

Merge Risk: ⚪ Minimal · up to 34fdd

No concrete current-head regression is established by the reviewed change; mergeability risk is minimal.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description check Passed The description includes the required Summary section and clearly explains the problem, impact, implementation, and validation results. Additional sections provide useful review context.
Title check Passed The title clearly and concisely describes the main change: preventing PDF TOC removal from deleting pages that contain short-line body text.
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 checks the pages in rows,
And counts the lines the body shows.
The leaders point, but do not count,
Short prose adds to the body amount.
The mixed page stays, each box in place,
The rabbit hops through TOC space.

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

@jay77721
jay77721 requested a review from xugangqiang October 15, 2026 03:59

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

LGTM at 34fdd02.

The body-text guard still runs before both outline-based and shape-based removal. Keeping the leading scan on long prose avoids stopping at a cover page, while the final guard protects the mixed TOC/body page. Leader-only boxes do not count as body evidence.

Ran CGO_ENABLED=0 go test ./internal/deepdoc/parser/pdf/layout -count=1 twice: PASS (0.809s, 0.769s). The five current CI checks passed. I did not independently rerun the real-PDF A/B comparison.

@xugangqiang

Copy link
Copy Markdown
Contributor

LGTM

@jay77721
jay77721 requested review from xugangqiang and yuzhichang and removed request for xugangqiang October 15, 2026 07:15
@yuzhichang
yuzhichang merged commit 5753d7b into infiniflow:main Oct 15, 2026
6 checks passed
@jay77721
jay77721 deleted the fix/pdf-toc-shortline-body-guard branch October 16, 2026 03:19
jay77721 added a commit to jay77721/ragflow that referenced this pull request Oct 30, 2026
OCR detection misses dotted TOC leader columns, so the native-text recovery
added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body
guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the
recovery touched was classified as a body page and kept: remove_toc silently
stopped removing the TOC of affected documents.

Treat the recovered shapes as what they are:

- a leader-only box is typographic filler and never prose;
- a box ending in leader runs plus a page number is one TOC entry, so it
  confirms the page as a marker instead of contradicting it as body text;
- a whole-box download-site ad line is not body evidence. Counting the ad
  lines of a piracy-supply PDF kept the last TOC page of such documents.

Entry lines reuse tocEntryRunPattern through isTOCEntryLine, so the
leader+number grammar stays in one place. The merged-block path still runs
first, so a long box holding several entries keeps its existing detection.
jay77721 added a commit to jay77721/ragflow that referenced this pull request Oct 30, 2026
OCR detection misses dotted TOC leader columns, so the native-text recovery
added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body
guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the
recovery touched was classified as a body page and kept: remove_toc silently
stopped removing the TOC of affected documents.

Treat the recovered shapes as what they are:

- a leader-only box is typographic filler and never prose;
- a box ending in leader runs plus a page number is one TOC entry, so it
  confirms the page as a marker instead of contradicting it as body text;
- a whole-box download-site ad line is not body evidence. Counting the ad
  lines of a piracy-supply PDF kept the last TOC page of such documents.

Entry lines reuse tocEntryRunPattern through isTOCEntryLine, so the
leader+number grammar stays in one place. The merged-block path still runs
first, so a long box holding several entries keeps its existing detection.
jay77721 added a commit to jay77721/ragflow that referenced this pull request Oct 30, 2026
OCR detection misses dotted TOC leader columns, so the native-text recovery
added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body
guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the
recovery touched was classified as a body page and kept: remove_toc silently
stopped removing the TOC of affected documents.

Treat the recovered shapes as what they are:

- a leader-only box is typographic filler and never prose;
- a box ending in leader runs plus a page number is one TOC entry, so it
  confirms the page as a marker instead of contradicting it as body text;
- a long box only counts as an entry when its leader run is real
  (tocMinLongEntryLeader), so prose ending in an ellipsis and a number keeps
  protecting its page;
- long entry lines count toward the list threshold, so a bookmarkless TOC
  made of long entry boxes is still detected;
- a whole-box download-site ad line is not body evidence. Counting the ad
  lines of a piracy-supply PDF kept the last TOC page of such documents.

Entry lines reuse tocEntryRunPattern through isTOCEntryLine, so the
leader+number grammar stays in one place. The merged-block path still runs
first, so a long box holding several entries keeps its existing detection.
jay77721 added a commit to jay77721/ragflow that referenced this pull request Oct 30, 2026
OCR detection misses dotted TOC leader columns, so the native-text recovery
added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body
guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the
recovery touched was classified as a body page and kept: remove_toc silently
stopped removing the TOC of affected documents.

Treat the recovered shapes as what they are:

- a leader-only box is typographic filler and never prose;
- a box ending in leader runs plus a page number is one TOC entry, so it
  confirms the page as a marker instead of contradicting it as body text;
- a long box only counts as an entry when its leader run is real
  (tocMinLongEntryLeader), so prose ending in an ellipsis and a number keeps
  protecting its page;
- long entry lines count toward the list threshold, so a bookmarkless TOC
  made of long entry boxes is still detected;
- a bare URL line is not body evidence. It is the running ad of a
  piracy-supply PDF; prose that merely mentions a link still counts.

The leader+number grammar accepts Roman-only page numbers and is reused by
isTOCEntryLine. The merged-block path still runs first, so a long box holding
several entries keeps its existing detection.
jay77721 added a commit to jay77721/ragflow that referenced this pull request Oct 30, 2026
OCR detection misses dotted TOC leader columns, so the native-text recovery
added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body
guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the
recovery touched was classified as a body page and kept: remove_toc silently
stopped removing the TOC of affected documents.

Treat the recovered shapes as what they are:

- a leader-only box is typographic filler and never prose;
- a box ending in leader runs plus a page number is one TOC entry, so it
  confirms the page as a marker instead of contradicting it as body text;
- a long box only counts as an entry when its leader run is real
  (tocMinLongEntryLeader), so prose ending in an ellipsis and a number keeps
  protecting its page;
- long entry lines count toward the list threshold, so a bookmarkless TOC
  made of long entry boxes is still detected;
- a bare URL that repeats on tocMinAdPages pages is a running ad and not body
  evidence. A one-off link (a reference, a links page) still counts, and so
  does prose that merely mentions a link.

The leader+number grammar accepts Roman-only page numbers and is reused by
isTOCEntryLine. The merged-block path still runs first, so a long box holding
several entries keeps its existing detection.
jay77721 added a commit to jay77721/ragflow that referenced this pull request Oct 30, 2026
OCR detection misses dotted TOC leader columns, so the native-text recovery
added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body
guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the
recovery touched was classified as a body page and kept: remove_toc silently
stopped removing the TOC of affected documents.

Treat the recovered shapes as what they are:

- a leader-only box is typographic filler and never prose;
- a box ending in leader runs plus a page number is one TOC entry, so it
  confirms the page as a marker instead of contradicting it as body text;
- leader segments split by spaces count as one run, so OCR-split leaders still
  reach the threshold;
- a long box only counts as an entry when its leader run is real
  (tocMinLongEntryLeader), and a short box needs tocMinShortEntryLeader, so
  prose ending in an ellipsis and a number keeps protecting its page;
- long entry lines count toward the list threshold, so a bookmarkless TOC
  made of long entry boxes is still detected;
- merged TOC blocks are confirmed entries, not prose, so several of them no
  longer guard their own page;
- a bare URL that repeats on tocMinAdPages pages is a running ad and not body
  evidence. A one-off link (a reference, a links page) still counts, and so
  does prose that merely mentions a link.

The leader+number grammar accepts Roman-only page numbers and is reused by
isTOCEntryLine. The merged-block path still runs first, so a long box holding
several entries keeps its existing detection.
jay77721 added a commit to jay77721/ragflow that referenced this pull request Oct 30, 2026
OCR detection misses dotted TOC leader columns, so the native-text recovery
added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body
guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the
recovery touched was classified as a body page and kept: remove_toc silently
stopped removing the TOC of affected documents.

Treat the recovered shapes as what they are:

- a leader-only box is typographic filler and never prose;
- a box ending in leader runs plus a page number is one TOC entry, so it
  confirms the page as a marker instead of contradicting it as body text;
- leader segments split by spaces count as one run, so OCR-split leaders still
  reach the threshold;
- a long box only counts as an entry when its leader run is real
  (tocMinLongEntryLeader), and a short box needs tocMinShortEntryLeader, so
  prose ending in an ellipsis and a number keeps protecting its page;
- long entry lines count toward the list threshold, so a bookmarkless TOC
  made of long entry boxes is still detected;
- merged TOC blocks are confirmed entries, not prose, so several of them no
  longer guard their own page.

The leader+number grammar accepts Roman-only page numbers and is reused by
isTOCEntryLine. The merged-block path still runs first, so a long box holding
several entries keeps its existing detection.
jay77721 added a commit to jay77721/ragflow that referenced this pull request Oct 30, 2026
OCR detection misses dotted TOC leader columns, so the native-text recovery
added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body
guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the
recovery touched was classified as a body page and kept: remove_toc silently
stopped removing the TOC of affected documents.

Treat the recovered shapes as what they are:

- a leader-only box is typographic filler and never prose;
- a box ending in leader runs plus a page number is one TOC entry, so it
  confirms the page as a marker instead of contradicting it as body text;
- leader segments split by spaces count as one run, so OCR-split leaders still
  reach the threshold;
- a long box only counts as an entry when its leader run is real
  (tocMinLongEntryLeader), and a short box needs tocMinShortEntryLeader, so
  prose ending in an ellipsis and a number keeps protecting its page;
- long entry lines count toward the list threshold, so a bookmarkless TOC
  made of long entry boxes is still detected;
- merged TOC blocks are confirmed entries, not prose, so several of them no
  longer guard their own page.

The leader+number grammar accepts Roman-only page numbers with a word
boundary, so prose words are not mistaken for page numbers, and is reused by
isTOCEntryLine. The merged-block path still runs first, so a long box holding
several entries keeps its existing detection.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug ci Continue Integration go Pull requests that update go code parsing size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants