Skip to content

Fix: parse negative coordinates in @@ position tags - #19945

Merged
yuzhichang merged 1 commit into
infiniflow:mainfrom
xugangqiang:fix/postag-negative-coord
Sep 20, 2026
Merged

yuzhichang merged 1 commit into
infiniflow:mainfrom
xugangqiang:fix/postag-negative-coord

Conversation

@xugangqiang

Copy link
Copy Markdown
Contributor

Summary

  • The posTagPattern regex in internal/deepdoc/parser/pdf/util/position.go used a coordinate class of [0-9.\t]+, which cannot match a leading minus sign. Any position tag with a negative coordinate (e.g. a content box extending slightly above the page top, top=-3.0) failed to parse.
  • As a result ExtractPositions returned an empty list and CropSectionImage silently dropped the section image with the cropSectionImage: empty position list warning — the section lost its cropped image in the QA/retrieval UI.
  • Negative coordinates are legitimate (a box can overflow the page origin); CropSectionImage already clamps out-of-bounds values via FastCrop. The fix allows - in the coordinate class so such tags parse and the section image is generated.

Root cause

Reported warning:

WARN cropSectionImage: empty position list in tag posTag="@@50\t45.0\t549.7\t-3.0\t737.9##"

The tag's top=-3.0 is negative. FormatPositionTag writes it verbatim, but ExtractPositions' regex rejected it — a writer/reader self-inconsistency. Each affected section failed in both the CropSectionPositions path and the direct CropSectionImage fallback, which is why the warning appeared twice per tag.

Test plan

  • Added TestExtractPositionsNegativeCoord and TestFormatPositionTagNegativeRoundtrip (position_test.go) plus TestCropSectionImage_NegativeCoordNotDropped (crop_test.go) covering negative coords and the format/parse roundtrip.
  • bash build.sh --test ./internal/deepdoc/parser/pdf/util/ and ./internal/parser/parser/ both pass; no regression.

Notes

  • Go-side fix only. The Python parser (deepdoc/parser/pdf_parser.py:1941) uses the same regex and has the same latent bug; a separate issue should track syncing it (out of scope per current phase constraints).

The posTagPattern regex used a coordinate class of [0-9.\t]+, which
cannot match a leading minus sign. Any position tag with a negative
coordinate (e.g. a content box extending slightly above the page top,
top=-3.0) failed to parse, so ExtractPositions returned an empty list
and CropSectionImage silently dropped the section image with the
'cropSectionImage: empty position list' warning.

Negative coordinates are legitimate (a box can overflow the page
origin); CropSectionImage already clamps out-of-bounds values via
FastCrop. Allow '-' in the coordinate class so such tags parse and the
section image is generated instead of being lost.

This is a Go-side fix. The Python parser (deepdoc/parser/pdf_parser.py)
uses the same regex and has the same latent bug; a separate issue should
track syncing it.

Co-authored-by: CodeBuddy <[email protected]>
@xugangqiang xugangqiang added the ci Continue Integration label Sep 20, 2026
@coderabbitai

coderabbitai Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview 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: 4c5cab3b-866c-453c-801e-e37d499bc10e

📥 Commits

Reviewing files that changed from the base of the PR and between 7f67d6e and a70728b.

📒 Files selected for processing (3)
  • internal/deepdoc/parser/pdf/util/crop_test.go
  • internal/deepdoc/parser/pdf/util/position.go
  • internal/deepdoc/parser/pdf/util/position_test.go

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The PDF position parser now accepts negative coordinates. Tests cover extraction, formatting roundtrips, and image cropping for a slightly negative top coordinate.

Changes

Negative Position Coordinates

Layer / File(s) Summary
Negative coordinate parsing and roundtrip coverage
internal/deepdoc/parser/pdf/util/position.go, internal/deepdoc/parser/pdf/util/position_test.go
posTagPattern accepts negative coordinate values. Tests verify extraction and formatting roundtrips, including page index conversion.
Negative-coordinate crop regression
internal/deepdoc/parser/pdf/util/crop_test.go
A regression test verifies that a crop with a slightly negative top coordinate returns decodable, non-degenerate PNG data.

Priority: ⬇️ Low

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

Change: Bug fix

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the main change: parsing negative coordinates in @@ position tags.
Description check ✅ Passed The description includes the required Summary section and provides clear background, root cause, implementation scope, tests, and known Python-parser follow-up.
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

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 finds a minus sign bright
And guides the crop through page-top light
The parser reads each point in line
The image blooms in PNG shine
Tests hop through the roundtrip gate

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

@yuzhichang
yuzhichang merged commit 3aba7b4 into infiniflow:main Sep 20, 2026
6 checks passed
@xugangqiang
xugangqiang deleted the fix/postag-negative-coord branch September 23, 2026 07:17
lihongyuan99 added a commit to lihongyuan99/ragflow that referenced this pull request Oct 2, 2026
…ython side

Sync the Python readers with the Go-side fix in infiniflow#19945, whose notes call
out this exact gap: the coordinate class [0-9.\t]+ cannot match a leading
minus, so tags like @@50<TAB>45.0<TAB>549.7<TAB>-3.0<TAB>737.9## produced
by Pdf._line_tag (top is offset by page_cum_height and can go negative)
were dropped by extract_positions (empty position list -> section image
silently lost) and left un-stripped by the chunk tokenizer, leaking raw
tags into chunk text. Allow '-' in the coordinate class at all nine read
sites, keeping reader and writer self-consistent.

Add regression tests driving the real RAGFlowPdfParser.extract_positions
and remove_tag with negative coordinates and page-range tags.
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.

2 participants