fix(shipping-notice): 跳过未选中页物理表避免空重建污染整单

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-09-08 19:33:55 +08:00
1 parent 76729df27c
commit c4a27324c8
2 files changed
+224 -3

No files matched your search

@@ -572,7 +572,8 @@ def _physical_table_targets_profile_region(
核心逻辑:同页页脚/签章等非目标 MinerU 表不得因重建失败阻断已成功目标表。
当前页已配置专属表区但全部没有 ``line_layouts``(仅表头壳)时明确跳过,
避免空重建产出 ``PARSE_STRATEGY_NO_VALID_ROW_RECONSTRUCTION`` 污染整单。
虚拟页、缺失 bbox 或页尺寸异常时仍 fail-open,避免表检测遗漏被错误跳过。
模板已有表区但本页未被运行时选中时,真实物理表同样跳过(虚拟页仍 fail-open)。
虚拟页、缺失 bbox、页尺寸异常或全无表区配置时仍 fail-open,避免表检测遗漏被错误跳过。
Args:
raw_table: 物理表身份、页码与 bbox。
@@ -609,7 +610,16 @@ def _physical_table_targets_profile_region(
and isinstance(region.get('region'), dict)
]
if not page_regions:
# 本页无模板表区配置:保守保留,供虚拟页/漏检回退。
# 模板已声明表区、但本页未被 select_runtime_region_ids 选中时,
# 真实 MinerU 表必须跳过:否则空重建会写入
# PARSE_STRATEGY_NO_VALID_ROW_RECONSTRUCTION 并 blocking 污染已成功目标页
# (FOX TW 仅 exact_page=2 时第 1 页表即属此类)。
# 虚拟页已在上方提前 return True;全无表区配置时仍 fail-open。
if any(
isinstance(region, dict) and str(region.get('region_id') or '')
for region in table_regions
):
return False
return True
# [STEP 2] 仅含 line_layouts 的明细区才是重建目标;纯表头壳直接跳过。
active_regions = [
@@ -3,7 +3,8 @@ tests/test_shipping_generic_order_gate.py
用途:锁定通用确定性解析完整性门禁的 fail-closed 行为与 workflow 路由接线。
依赖:generic_order_gate、generic_order_parser、order_extraction_workflow。
逻辑:未映射列、多客户、续行与必填字段缺失必须阻断;layout_drift 禁止降级。
逻辑:未映射列、多客户、续行与必填字段缺失必须阻断;layout_drift 禁止降级;
同时回归坐标模板非目标页物理表不得污染 PARSE_STRATEGY_NO_VALID_ROW。
"""
from __future__ import annotations
@@ -85,6 +86,216 @@ def test_header_only_exact_page_region_is_not_a_parse_target():
) is False
def test_exact_page_two_profile_skips_real_tables_on_unconfigured_pages():
"""FOX TW 类模板仅 exact_page=2:第 1 页真实 MinerU 表不得 fail-open 进入空重建。"""
regions = [{
'region_id': 'main_lines_page_2', 'page_scope': 'exact_page', 'page_no': 2,
'region': {'x0': .02, 'y0': .22, 'x1': .97, 'y1': .28},
'line_layouts': [{'layout_id': 'line_2_1'}],
}]
page = {'width': 595, 'height': 842}
assert _physical_table_targets_profile_region(
raw_table={
'table_id': 'mineru-page1-header', 'page_no': 1,
'bbox': {'x0': 20, 'y0': 100, 'x1': 560, 'y1': 180},
},
table_regions=regions, page_size=page,
) is False
assert _physical_table_targets_profile_region(
raw_table={
'table_id': 'mineru-table-page2', 'page_no': 2,
'bbox': {'x0': 13, 'y0': 185, 'x1': 578, 'y1': 236},
},
table_regions=regions, page_size=page,
) is True
# 虚拟页仍 fail-open,供表检测漏检回退。
assert _physical_table_targets_profile_region(
raw_table={'table_id': 'page-1', 'page_no': 1, 'bbox': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1}},
table_regions=regions, page_size=page,
) is True
def test_other_page_scopes_still_target_real_tables_on_selected_pages():
"""回归:first_page / all_pages / continuation_pages 选中页的真实表仍应进入重建。"""
page = {'width': 100, 'height': 100}
overlapping = {'table_id': 'mineru-lines', 'page_no': 1, 'bbox': {'x0': 10, 'y0': 40, 'x1': 90, 'y1': 55}}
first_page = [{
'region_id': 'lines', 'page_scope': 'first_page',
'region': {'x0': .1, 'y0': .4, 'x1': .9, 'y1': .55},
'line_layouts': [{}],
}]
assert _physical_table_targets_profile_region(
raw_table=overlapping, table_regions=first_page, page_size=page,
) is True
# first_page 在续页会回退复用;几何相交时仍是目标。
assert _physical_table_targets_profile_region(
raw_table={**overlapping, 'page_no': 2, 'table_id': 'mineru-lines-p2'},
table_regions=first_page, page_size=page,
) is True
all_pages = [{
'region_id': 'lines', 'page_scope': 'all_pages',
'region': {'x0': .1, 'y0': .4, 'x1': .9, 'y1': .55},
'line_layouts': [{}],
}]
assert _physical_table_targets_profile_region(
raw_table={**overlapping, 'page_no': 3, 'table_id': 'mineru-lines-p3'},
table_regions=all_pages, page_size=page,
) is True
continuation = [
{
'region_id': 'page_one', 'page_scope': 'first_page',
'region': {'x0': .1, 'y0': .4, 'x1': .9, 'y1': .55},
'line_layouts': [{}],
},
{
'region_id': 'cont', 'page_scope': 'continuation_pages',
'region': {'x0': .1, 'y0': .2, 'x1': .9, 'y1': .35},
'line_layouts': [{}],
},
]
assert _physical_table_targets_profile_region(
raw_table={
'table_id': 'mineru-cont', 'page_no': 2,
'bbox': {'x0': 10, 'y0': 20, 'x1': 90, 'y1': 34},
},
table_regions=continuation, page_size=page,
) is True
def test_profile_without_table_regions_keeps_fail_open_for_real_tables():
"""无任何表区配置时仍保守保留真实表,避免误伤未完成模板草案。"""
assert _physical_table_targets_profile_region(
raw_table={
'table_id': 'mineru-any', 'page_no': 1,
'bbox': {'x0': 10, 'y0': 10, 'x1': 90, 'y1': 40},
},
table_regions=[], page_size={'width': 100, 'height': 100},
) is True
def test_exact_page_two_non_target_page_tables_do_not_block_successful_parse(monkeypatch):
"""关键断言:第 1 页非目标实表不得把 NO_VALID_ROW 污染到第 2 页成功解析。"""
profile = {
'profile_id': 4,
'profile_code': 'ODP-FOX-TW',
'version': 1,
'checksum': 'c' * 64,
'schema_version_id': 31,
'auto_create_approved': True,
'definition': {
'table_regions': [{
'region_id': 'main_lines_page_2', 'page_scope': 'exact_page', 'page_no': 2,
'region': {'x0': .02, 'y0': .22, 'x1': .97, 'y1': .28},
'line_layouts': [{'layout_id': 'line_2_1'}],
}],
},
}
document = {
'document_id': 208,
'layout_snapshot': {
'status': 'ready',
'layout_candidate': {'pages': []},
'coordinate_evidence': {
'pages': {
'1': {'width': 595, 'height': 842},
'2': {'width': 595, 'height': 842},
},
'page_glyphs': {'1': [{}], '2': [{}]},
},
'tables': [
{
'table_id': 'mineru-page1-a', 'page_no': 1,
'bbox': {'x0': 20, 'y0': 100, 'x1': 560, 'y1': 180},
},
{
'table_id': 'mineru-page1-b', 'page_no': 1,
'bbox': {'x0': 20, 'y0': 200, 'x1': 560, 'y1': 280},
},
{
'table_id': 'mineru-page2-lines', 'page_no': 2,
'bbox': {'x0': 13, 'y0': 185, 'x1': 578, 'y1': 236},
},
],
},
}
run = SimpleNamespace(
schema_version_id=31,
input_snapshot_json={
'schema': {'definition': {'fields': []}},
'order_document_profiles': [profile],
'intakes': [{'intake_no': 'SNI-FOX', 'documents': [document]}],
},
)
unit = SimpleNamespace(document_id=208, intake_id=1)
match = SimpleNamespace(
status='unique_match', profile_code='ODP-FOX-TW', version=1, checksum='c' * 64,
model_dump=lambda **_kwargs: {
'status': 'unique_match', 'profile_code': 'ODP-FOX-TW', 'version': 1,
'checksum': 'c' * 64, 'candidates': [],
},
)
region = SimpleNamespace(
region_id='main_lines_page_2',
line_layouts=[object()],
continuation_rule=SimpleNamespace(allow_cross_page=False),
region=SimpleNamespace(
model_dump=lambda: {'x0': .02, 'y0': .22, 'x1': .97, 'y1': .28},
),
model_dump=lambda: {
'region_id': 'main_lines_page_2', 'page_scope': 'exact_page', 'page_no': 2,
'region': {'x0': .02, 'y0': .22, 'x1': .97, 'y1': .28},
'line_layouts': [{'layout_id': 'line_2_1'}],
},
)
monkeypatch.setattr(
'app.services.shipping_notice.order_extraction_workflow.resolve_unit_runtime_snapshot',
lambda *_args, **_kwargs: ({'intake_no': 'SNI-FOX'}, document),
)
monkeypatch.setattr(
'app.services.shipping_notice.order_extraction_workflow.match_frozen_coordinate_profiles',
lambda **_kwargs: match,
)
monkeypatch.setattr(
'app.services.shipping_notice.order_extraction_workflow._profile_snapshot_targets_valid',
lambda *_args, **_kwargs: True,
)
monkeypatch.setattr(
'app.services.shipping_notice.order_extraction_workflow._frozen_coordinate_profile',
lambda *_args, **_kwargs: SimpleNamespace(
table_regions=[region], reference_page_frames={},
),
)
seen_table_ids: list[str] = []
def _fake_rows(**kwargs):
table_id = kwargs['raw_table']['table_id']
seen_table_ids.append(table_id)
if table_id != 'mineru-page2-lines':
return []
return [{
'row_index': 0, 'status': 'parsed',
'fields': [{'field_id': 'part-id', 'field_code': 'part_number', 'value': 'D4-A08'}],
'issues': [], '_coordinate_region_id': 'main_lines_page_2', '_coordinate_row_count': 1,
}]
monkeypatch.setattr(
'app.services.shipping_notice.order_extraction_workflow._coordinate_profile_rows',
_fake_rows,
)
result = _profile_table_candidates(run, unit=unit)
assert result['blocking'] is False
assert 'PARSE_STRATEGY_NO_VALID_ROW_RECONSTRUCTION' not in result['issues']
assert len(result['tables']) == 1
assert result['tables'][0]['table_id'] == 'mineru-page2-lines'
assert seen_table_ids == ['mineru-page2-lines'] * 3
def test_empty_virtual_page_does_not_block_a_successful_target_table():
"""无物理表的续页仅用于补漏;没有行时不得覆盖已成功的目标表。"""
assert _virtual_page_without_rows_is_nonblocking(