fix(shipping-notice): 抽取收口纠正失败文档状态并修复 OCR 救援资产读取

- 抽取收口读取 Run 内 Unit 级失败事实:全部 Unit 失败的附件不再按零 pending 误写 completed
- EML 父项跟随 Run 结果收敛:终态 Run 禁止停留「处理中」,运行中仅区分解析失败与处理中
- OCR 救援资产读取修复:read_asset_bytes 接收 id 时按 FileAsset 加载字节,传参口径纠正
- 同步抽取/投影测试并新增工作台投影测试
This commit is contained in:
hugo committed 2026-10-14 22:40:37 +08:00
1 parent 92ed140739
commit 9f132564d6
8 files changed
+480 -39

No files matched your search

@@ -17,6 +17,7 @@ backend/app/services/shipping_notice/document_layout/order_profile_ocr_rescue.py
from __future__ import annotations
import logging
import unicodedata
from typing import Any, Callable
@@ -38,6 +39,8 @@ from app.services.shipping_notice.document_layout.order_profile import (
OrderCoordinateProfile,
)
logger = logging.getLogger(__name__)
# 与 ocr_pdf.OCR_CONFIDENCE_FLOOR 对齐;低于该置信度的 OCR 行不参与补洞。
OCR_RESCUE_CONFIDENCE_FLOOR = 0.8
# 表头带高度占主表高度的比例,与 _observe_header_candidates 的保守带一致。
@@ -194,6 +197,18 @@ def _text_to_char_glyphs(text: str, bbox: dict[str, float]) -> list[dict[str, An
return out
def _default_read_asset_bytes(asset_id: int) -> bytes:
"""按 id 加载 FileAsset 后读取字节;read_asset_bytes 接收 ORM 对象而非 id。"""
from app.extensions import db
from app.models import FileAsset
from app.services.file_assets.service import read_asset_bytes
asset = db.session.get(FileAsset, int(asset_id))
if asset is None:
raise LookupError(f'file asset not found: {asset_id}')
return read_asset_bytes(asset)
def _collect_rescue_glyphs(
*,
document: Any,
@@ -213,6 +228,8 @@ def _collect_rescue_glyphs(
page_width = float(page.get('width') or 0)
page_height = float(page.get('height') or 0)
asset_id = int(getattr(document, 'asset_id', 0) or 0)
if read_asset_fn is None:
read_asset_fn = _default_read_asset_bytes
# [STEP 1] AcroForm/XFA 控件文本:确定性坐标与文本,优先于 OCR。
if asset_id > 0:
try:
@@ -247,9 +264,6 @@ def _collect_rescue_glyphs(
if asset_id <= 0:
return None
try:
if read_asset_fn is None:
from app.services.file_assets.service import read_asset_bytes
read_asset_fn = read_asset_bytes
if page_images_fn is None:
from app.services.shipping_notice.document_layout.native_pdf import (
extract_native_pdf_pages,
@@ -296,7 +310,15 @@ def _collect_rescue_glyphs(
if band:
return band, 'ocr_coordinate'
return None
except (ValueError, TypeError, KeyError, OSError):
except Exception as exc: # noqa: BLE001 - 兜底来源失败时交回上层维持原判
# [PARAM_TUNING] 2026-10-14 test 事故(SNER-20261014-8EBAB7A1):AttributeError
# 炸穿本层令整个 Unit 技术失败。收口语义保持「静默回退」,但必须留观测痕
# 迹,否则只能靠 Unit error_message 反推。
logger.warning(
'order profile rescue glyphs failed document_id=%s error_type=%s',
int(getattr(document, 'id', 0) or 0),
type(exc).__name__,
)
return None
@@ -341,9 +363,6 @@ def rescue_match_with_header_ocr(
page = pages.get('1') or pages.get(1)
if not isinstance(page, dict):
return match
if read_asset_fn is None:
from app.services.file_assets.service import read_asset_bytes
read_asset_fn = read_asset_bytes
_, _, probe_observed = _candidate_header_atoms(
raw_profile=next(iter(eligible.values()))[1],
layout=layout,
@@ -390,6 +409,10 @@ def rescue_match_with_header_ocr(
'identity_rescue_source': rescue_source,
'candidates': marked_candidates,
})
except (ValueError, TypeError, KeyError, OSError):
# 补救属锦上添花:任何数据/环境异常都回退原判,不阻断主链。
except Exception as exc: # noqa: BLE001 - 补救属锦上添花:任何异常(含 AttributeError 等未预期类型错误)都回退原判,不得阻断抽取主链。
logger.warning(
'order profile rescue aborted document_id=%s error_type=%s',
int(getattr(document, 'id', 0) or 0),
type(exc).__name__,
)
return match