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(deepdoc): clamp TSR/DLA boxes to image bounds at the analyzer exit (#20836)
xugangqiang438df385d666
diff --git a/internal/deepdoc/native/dla.go b/internal/deepdoc/native/dla.goindex 5cf976427d..a98d662e2e 100644--- a/internal/deepdoc/native/dla.go+++ b/internal/deepdoc/native/dla.go@@ -250,18 +250,31 @@ func dlaPostprocess(out []float32, sf [4]float32) DLAResult {return res}+// ClampBoxes returns a copy of the boxes with every coordinate clamped into+// the source image bounds [0, W] x [0, H] (mirrors dla_adapter.py). The+// in-process DocAnalyzer backend consumes these boxes directly instead of+// serialising through Wire(), so the clamp must live on the result itself.+// The receiver's boxes are never mutated: Wire() and the analyzer share one+// result and both need the raw postprocess values intact.+func (r DLAResult) ClampBoxes() []DLABox {+ w, h := float32(r.W), float32(r.H)+ out := make([]DLABox, len(r.Boxes))+ for i, b := range r.Boxes {+ b.X0 = minf(maxf(b.X0, 0), w)+ b.Y0 = minf(maxf(b.Y0, 0), h)+ b.X1 = minf(maxf(b.X1, 0), w)+ b.Y1 = minf(maxf(b.Y1, 0), h)+ out[i] = b+ }+ return out+}+// Wire encodes the result in the exact format the Go DocAnalyzer consumes:// {"bboxes": [[x0,y0,x1,y1,score,class_id], ...]}.func (r DLAResult) Wire() string {rows := make([][]float32, 0, len(r.Boxes))- w, h := float32(r.W), float32(r.H)- for _, b := range r.Boxes {- // Clamp into image bounds (mirrors dla_adapter.py).- x0 := minf(maxf(b.X0, 0), w)- y0 := minf(maxf(b.Y0, 0), h)- x1 := minf(maxf(b.X1, 0), w)- y1 := minf(maxf(b.Y1, 0), h)- rows = append(rows, []float32{x0, y0, x1, y1, b.Score, float32(b.Class)})+ for _, b := range r.ClampBoxes() {+ rows = append(rows, []float32{b.X0, b.Y0, b.X1, b.Y1, b.Score, float32(b.Class)})}b, _ := json.Marshal(map[string]any{"bboxes": rows})return string(b)diff --git a/internal/deepdoc/native/native_test.go b/internal/deepdoc/native/native_test.goindex f8d19201f9..9a05fdcb8c 100644--- a/internal/deepdoc/native/native_test.go+++ b/internal/deepdoc/native/native_test.go@@ -112,6 +112,37 @@ func TestDLAWireClamps(t *testing.T) {}}+func TestDLAResultClampBoxes(t *testing.T) {+ // Same contract as TSR: the in-process backend consumes raw postprocess+ // boxes, so the [0,W]/[0,H] clamp (mirroring dla_adapter.py) must live+ // on the result, not only in Wire().+ r := DLAResult{+ Boxes: []DLABox{+ {X0: -2, Y0: 1.5, X1: 103.75, Y1: 8, Score: 0.6, Class: 5},+ {X0: 10, Y0: 2, X1: 90, Y1: 8, Score: 0.7, Class: 1},+ },+ W: 100, H: 10,+ }+ got := r.ClampBoxes()+ if len(got) != 2 {+ t.Fatalf("ClampBoxes returned %d boxes, want 2", len(got))+ }+ b := got[0]+ if b.X0 != 0 || b.Y0 != 1.5 || b.X1 != 100 || b.Y1 != 8 {+ t.Errorf("ClampBoxes[0] = (%v, %v, %v, %v), want (0, 1.5, 100, 8)", b.X0, b.Y0, b.X1, b.Y1)+ }+ // In-bounds boxes pass through untouched.+ in := got[1]+ if in.X0 != 10 || in.Y0 != 2 || in.X1 != 90 || in.Y1 != 8 {+ t.Errorf("ClampBoxes[1] = (%v, %v, %v, %v), want (10, 2, 90, 8)", in.X0, in.Y0, in.X1, in.Y1)+ }+ // The receiver must not be mutated: Wire() and the analyzer share one+ // result and both need the raw values intact.+ if r.Boxes[0].X0 != -2 || r.Boxes[0].X1 != 103.75 {+ t.Errorf("ClampBoxes mutated the receiver's boxes: %+v", r.Boxes[0])+ }+}+func TestTSRWire(t *testing.T) {r := TSRResult{Boxes: []TSRBox{{Label: "table column", Score: 0.7, X0: 1.2, X1: 9.8, Top: 3.4, Bottom: 5.6}}, W: 1000, H: 1000}// "table column" -> class 1; within bounds so clamp is a no-op.@@ -132,6 +163,38 @@ func TestTSRWireClamps(t *testing.T) {}}+func TestTSRResultClampBoxes(t *testing.T) {+ // The in-process DocAnalyzer backend consumes raw postprocess boxes+ // directly and never serialises through Wire(), so the [0,W]/[0,H]+ // clamp (mirroring tsr_adapter.py) must live on the result itself.+ // A bottom-edge row at 370.18 on a 370-tall image must come back at 370.+ r := TSRResult{+ Boxes: []TSRBox{+ {Label: "table row", Score: 0.9, X0: -4.5, X1: 12.25, Top: 3.4, Bottom: 370.18},+ {Label: "table column", Score: 0.8, X0: 1.2, X1: 9.8, Top: 3.4, Bottom: 5.6},+ },+ W: 10, H: 370,+ }+ got := r.ClampBoxes()+ if len(got) != 2 {+ t.Fatalf("ClampBoxes returned %d boxes, want 2", len(got))+ }+ b := got[0]+ if b.X0 != 0 || b.X1 != 10 || b.Top != 3.4 || b.Bottom != 370 {+ t.Errorf("ClampBoxes[0] = (%v, %v, %v, %v), want (0, 10, 3.4, 370)", b.X0, b.X1, b.Top, b.Bottom)+ }+ // In-bounds boxes pass through untouched.+ in := got[1]+ if in.X0 != 1.2 || in.X1 != 9.8 || in.Top != 3.4 || in.Bottom != 5.6 {+ t.Errorf("ClampBoxes[1] = (%v, %v, %v, %v), want (1.2, 9.8, 3.4, 5.6)", in.X0, in.X1, in.Top, in.Bottom)+ }+ // The receiver must not be mutated: Wire() and the analyzer share one+ // result and both need the raw values intact.+ if r.Boxes[0].X0 != -4.5 || r.Boxes[0].Bottom != 370.18 {+ t.Errorf("ClampBoxes mutated the receiver's boxes: %+v", r.Boxes[0])+ }+}+func TestOCRRecWire(t *testing.T) {r := OCRRecResult{Text: "hello", Score: 1.0}got := r.Wire()diff --git a/internal/deepdoc/native/tsr.go b/internal/deepdoc/native/tsr.goindex d65af2e620..b86db22348 100644--- a/internal/deepdoc/native/tsr.go+++ b/internal/deepdoc/native/tsr.go@@ -265,22 +265,35 @@ var tsrClassMap = map[string]int{"table column header": 3, "table projected row header": 4, "table spanning cell": 5,}+// ClampBoxes returns a copy of the boxes with every coordinate clamped into+// the source image bounds [0, W] x [0, H] (mirrors tsr_adapter.py). The+// in-process DocAnalyzer backend consumes these boxes directly instead of+// serialising through Wire(), so the clamp must live on the result itself.+// The receiver's boxes are never mutated: Wire() and the analyzer share one+// result and both need the raw postprocess values intact.+func (r TSRResult) ClampBoxes() []TSRBox {+ w, h := float32(r.W), float32(r.H)+ out := make([]TSRBox, len(r.Boxes))+ for i, b := range r.Boxes {+ b.X0 = minf(maxf(b.X0, 0), w)+ b.X1 = minf(maxf(b.X1, 0), w)+ b.Top = minf(maxf(b.Top, 0), h)+ b.Bottom = minf(maxf(b.Bottom, 0), h)+ out[i] = b+ }+ return out+}+// Wire emits the Go DocAnalyzer TSR format:// {"bboxes": [[x0,y0,x1,y1,score,class_id], ...]}.func (r TSRResult) Wire() string {rows := make([][]float32, 0, len(r.Boxes))- w, h := float32(r.W), float32(r.H)- for _, b := range r.Boxes {+ for _, b := range r.ClampBoxes() {cls, ok := tsrClassMap[b.Label]if !ok {continue}- // Clamp into image bounds (mirrors tsr_adapter.py).- x0 := minf(maxf(b.X0, 0), w)- x1 := minf(maxf(b.X1, 0), w)- top := minf(maxf(b.Top, 0), h)- bot := minf(maxf(b.Bottom, 0), h)- rows = append(rows, []float32{x0, top, x1, bot, b.Score, float32(cls)})+ rows = append(rows, []float32{b.X0, b.Top, b.X1, b.Bottom, b.Score, float32(cls)})}out, _ := json.Marshal(map[string]any{"bboxes": rows})return string(out)diff --git a/internal/deepdoc/parser/pdf/inference/native_analyzer/native_analyzer.go b/internal/deepdoc/parser/pdf/inference/native_analyzer/native_analyzer.goindex 754d405d57..35fbe4d478 100644--- a/internal/deepdoc/parser/pdf/inference/native_analyzer/native_analyzer.go+++ b/internal/deepdoc/parser/pdf/inference/native_analyzer/native_analyzer.go@@ -118,7 +118,10 @@ func (a *NativeAnalyzer) DLA(ctx context.Context, img image.Image) ([]deepdoctyp}labels := deepdoctype.DefaultDLALabels()out := make([]deepdoctype.DLARegion, 0, len(res.Boxes))- for _, b := range res.Boxes {+ // Clamp into the source image bounds (mirrors dla_adapter.py): this+ // backend replaced the HTTP service whose adapter clamped every+ // coordinate, and its consumers never see the raw postprocess values.+ for _, b := range res.ClampBoxes() {label := ""if b.Class >= 0 && int(b.Class) < len(labels) {label = labels[b.Class]@@ -144,7 +147,10 @@ func (a *NativeAnalyzer) TSR(ctx context.Context, img image.Image) ([]deepdoctypreturn nil, err}out := make([]deepdoctype.TSRCell, 0, len(res.Boxes))- for _, b := range res.Boxes {+ // Clamp into the source image bounds (mirrors tsr_adapter.py): this+ // backend replaced the HTTP service whose adapter clamped every+ // coordinate, and its consumers never see the raw postprocess values.+ for _, b := range res.ClampBoxes() {out = append(out, deepdoctype.TSRCell{X0: float64(b.X0), Y0: float64(b.Top),X1: float64(b.X1), Y1: float64(b.Bottom),diff --git a/internal/deepdoc/parser/pdf/inference/native_analyzer/native_analyzer_test.go b/internal/deepdoc/parser/pdf/inference/native_analyzer/native_analyzer_test.goindex d80c4762a7..abfb3f9346 100644--- a/internal/deepdoc/parser/pdf/inference/native_analyzer/native_analyzer_test.go+++ b/internal/deepdoc/parser/pdf/inference/native_analyzer/native_analyzer_test.go@@ -131,6 +131,18 @@ func TestNativeAnalyzerInProcess(t *testing.T) {if err != nil {t.Fatalf("TSR: %v", err)}+ for _, c := range tsr {+ if c.X1 < c.X0 || c.Y1 < c.Y0 {+ t.Errorf("TSR cell has inverted bounds: %+v", c)+ }+ // The analyzer must hand consumers coordinates clamped into the+ // source image (mirroring tsr_adapter.py); tsrPostprocess itself+ // does not clamp, so a bottom-edge box can otherwise come back at+ // e.g. 370.18 on a 370-tall crop.+ if c.X0 < 0 || c.Y0 < 0 || c.X1 > w || c.Y1 > h {+ t.Errorf("TSR cell out of image bounds (%dx%d): %+v", int(w), int(h), c)+ }+ }t.Logf("TSR: %d cells", len(tsr))det, err := a.OCRDetect(ctx, src)