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: parse negative coordinates in @@ position tags (#19945)
xugangqiang3aba7b47a5aa
diff --git a/internal/deepdoc/parser/pdf/util/crop_test.go b/internal/deepdoc/parser/pdf/util/crop_test.goindex f8d6425dc3..dd24e15d19 100644--- a/internal/deepdoc/parser/pdf/util/crop_test.go+++ b/internal/deepdoc/parser/pdf/util/crop_test.go@@ -90,6 +90,29 @@ func TestCropSectionImage_InvalidTag(t *testing.T) {}}+func TestCropSectionImage_NegativeCoordNotDropped(t *testing.T) {+ // Regression: a content box extending slightly above the page top+ // (top=-3.0) must still produce a cropped image instead of being dropped+ // with "cropSectionImage: empty position list". This is the exact tag that+ // previously triggered the warning.+ pageImages := map[int]image.Image{+ 49: makeTestPageImage(600, 800, color.RGBA{255, 0, 0, 255}),+ }+ posTag := FormatPositionTag(49, 45.0, 549.7, -3.0, 737.9) // -> "@@50\t45.0\t549.7\t-3.0\t737.9##"+ b64 := CropSectionImage(posTag, pageImages, 1)+ if b64 == "" {+ t.Fatal("negative-coord tag must produce an image, not be dropped")+ }+ decoded, err := base64.StdEncoding.DecodeString(b64)+ if err != nil {+ t.Fatalf("base64 decode: %v", err)+ }+ img := decodePNG(t, decoded)+ if img.Bounds().Dx() <= 0 || img.Bounds().Dy() <= 0 {+ t.Errorf("decoded image is degenerate: %v", img.Bounds())+ }+}+func TestCropSectionImage_ContextPadding(t *testing.T) {pageImages := map[int]image.Image{0: makeTestPageImage(200, 800, color.RGBA{255, 0, 0, 255}),diff --git a/internal/deepdoc/parser/pdf/util/position.go b/internal/deepdoc/parser/pdf/util/position.goindex 100a75cb3a..380eefaa45 100644--- a/internal/deepdoc/parser/pdf/util/position.go+++ b/internal/deepdoc/parser/pdf/util/position.go@@ -16,7 +16,13 @@ import (// posTagPattern matches the full @@...## tag including coordinates.// Format: @@{page_range}\t{left}\t{right}\t{top}\t{bottom}##-var posTagPattern = regexp.MustCompile(`@@[0-9-]+\t[0-9.\t]+##`)+//+// The coordinate class allows '-' so that negative coordinates (a content+// box extending above/left of the page origin, e.g. top=-3.0) are parsed+// instead of being dropped. The page-range segment still owns the only '-'+// that denotes a range (e.g. "0-2"); coordinates are tab-separated, so a '-'+// there can never be confused with a range delimiter.+var posTagPattern = regexp.MustCompile(`@@[0-9-]+\t[-0-9.\t]+##`)// ExtractPositions parses @@ position tags from a text string.//diff --git a/internal/deepdoc/parser/pdf/util/position_test.go b/internal/deepdoc/parser/pdf/util/position_test.goindex 88209d052a..abc4c37410 100644--- a/internal/deepdoc/parser/pdf/util/position_test.go+++ b/internal/deepdoc/parser/pdf/util/position_test.go@@ -79,3 +79,45 @@ func TestFormatPositionTagRange(t *testing.T) {t.Errorf("FormatPositionTagRange = %q", tag)}}++func TestExtractPositionsNegativeCoord(t *testing.T) {+ // A content box can extend slightly above/left of the page origin+ // (e.g. top=-3.0). The parser must accept negative coordinates instead+ // of dropping the whole tag (which previously lost the section image and+ // emitted "cropSectionImage: empty position list").+ text := "@@50\t45.0\t549.7\t-3.0\t737.9##"+ poss := ExtractPositions(text)+ if len(poss) != 1 {+ t.Fatalf("expected 1 position for negative coord tag, got %d", len(poss))+ }+ p := poss[0]+ if len(p.PageNumbers) != 1 || p.PageNumbers[0] != 49 {+ t.Errorf("page numbers = %v, want [49]", p.PageNumbers)+ }+ if p.Left != 45.0 || p.Right != 549.7 || p.Top != -3.0 || p.Bottom != 737.9 {+ t.Errorf("coords L=%.1f R=%.1f T=%.1f B=%.1f, want 45.0/549.7/-3.0/737.9",+ p.Left, p.Right, p.Top, p.Bottom)+ }+}++func TestFormatPositionTagNegativeRoundtrip(t *testing.T) {+ // FormatPositionTag writes negative coordinates verbatim (e.g. top=-3.0),+ // so ExtractPositions must be able to read them back. This was the+ // self-inconsistency: the writer produced tags the reader rejected.+ tag := FormatPositionTag(49, 45.0, 549.7, -3.0, 737.9)+ want := "@@50\t45.0\t549.7\t-3.0\t737.9##"+ if tag != want {+ t.Fatalf("FormatPositionTag = %q, want %q", tag, want)+ }+ poss := ExtractPositions(tag)+ if len(poss) != 1 {+ t.Fatalf("roundtrip of negative coord failed: got %d positions, want 1", len(poss))+ }+ p := poss[0]+ if p.Top != -3.0 {+ t.Errorf("roundtrip Top = %v, want -3.0", p.Top)+ }+ if p.PageNumbers[0] != 49 {+ t.Errorf("roundtrip page = %v, want 49", p.PageNumbers[0])+ }+}