From bc5388af6b888be8285896ae9dece0e654eec901 Mon Sep 17 00:00:00 2001 From: Richard R Date: Wed, 3 Jun 2026 04:35:27 -0600 Subject: [PATCH] fix(pdf): add strict validation for layout model output shapes and extend test coverage Add explicit error handling for invalid or inconsistent pred_boxes and logits array lengths in runLayoutModel to prevent silent failures. Expand test suite to verify correct region filtering and error scenarios, ensuring only labeled regions are returned and malformed outputs are handled robustly. --- compute/core/src/pdf/runLayoutModel.ts | 26 +++++++++++-- .../control-plane/run-layout-model.test.ts | 39 +++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/compute/core/src/pdf/runLayoutModel.ts b/compute/core/src/pdf/runLayoutModel.ts index c97dd6b..5c726e0 100644 --- a/compute/core/src/pdf/runLayoutModel.ts +++ b/compute/core/src/pdf/runLayoutModel.ts @@ -286,11 +286,29 @@ export async function runLayoutModel(input: RunLayoutInput): Promise { expect.closeTo(40, 5), ]); }); + + test('drops unlabeled query winners and keeps only labeled regions', async () => { + mockState.runOutput = { + logits: { + data: new Float32Array([ + 0.1, + 0.2, + 5, + 4, + 0.1, + 0.1, + ]), + }, + pred_boxes: { + data: new Float32Array([ + 0.25, 0.25, 0.3, 0.3, + 0.75, 0.75, 0.3, 0.3, + ]), + }, + }; + + const { runLayoutModel } = await import('../../src/pdf/runLayoutModel'); + const regions = await runLayoutModel({ + pageWidth: 100, + pageHeight: 100, + textItems: [{} as never], + pageImage: Buffer.from([1]), + }); + + expect(regions).toHaveLength(1); + expect(regions[0]?.label).toBe('text'); + expect(regions[0]?.confidence).toBeCloseTo(0.96109135, 5); + expect(regions[0]?.bbox).toEqual([ + expect.closeTo(60, 5), + expect.closeTo(60, 5), + expect.closeTo(90, 5), + expect.closeTo(90, 5), + ]); + }); });