feat(shipping-notice): 收敛复核阶段同源投影与 OT 后履约推进

统一 Case/Progress 阶段 label_key、修复 Job 成功未推进 OT 的可重试失败投影与写路径,并落地 OT 后行级匹配/波次履约与版面诊断开关。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-08-07 13:11:14 +08:00
1 parent 74270adc22
commit fd6a23f870
100 files changed
+12138 -1017

No files matched your search

@@ -2,9 +2,11 @@
backend/app/jobs/handlers/shipping_document_layout.py
用途:独立执行单份 ShippingOrderDocument 的 MinerU 布局解析任务。
依赖:Layout lifecycle/runtime、FileAsset、通用 Job queue、audit_service、workflow_ledger(尽力引用)。
依赖:Layout lifecycle/runtime、layout_diagnostics、FileAsset、通用 Job queue、audit_service、
workflow_ledger(尽力引用)。
逻辑:按 job attempt 领取版本 lease,读取对象存储调用 MinerU;收口时旧 lease 无权覆盖新版本;
起止与异常轻量写统一审计(若能定位 WorkflowItem 则带软引用)。
起止与异常轻量写统一审计(若能定位 WorkflowItem 则带软引用);
PR-0 将脱敏 timings/counts 写入 Job result 与 worker 日志,不进 layout snapshot。
"""
from __future__ import annotations
@@ -18,6 +20,12 @@ from app.jobs.registry import register_handler
from app.jobs.types import JobType
from app.models import FileAsset, Job, ShippingDocumentLayoutVersion
from app.services.file_assets.service import read_asset_bytes
from app.services.shipping_notice.document_layout.layout_diagnostics import (
empty_layout_counts,
empty_layout_timings,
freeze_layout_diagnostics,
ocr_engine_is_warm,
)
from app.services.shipping_notice.document_layout.lifecycle import (
claim_layout_version,
complete_layout_version,
@@ -139,11 +147,14 @@ def run_shipping_document_layout(ctx: JobContext, payload: dict) -> None:
total_started = perf_counter()
asset_started = perf_counter()
content = read_asset_bytes(asset)
timings = {
'queue_wait_ms': max(round((job.started_at - job.created_at).total_seconds() * 1000), 0)
if job.started_at and job.created_at else 0,
'asset_read_ms': round((perf_counter() - asset_started) * 1000),
}
# [STEP 1] 初始化全量诊断键;ocr_engine_warm 在任何 OCR 之前读取进程状态。
timings = empty_layout_timings()
counts = empty_layout_counts()
counts['ocr_engine_warm'] = ocr_engine_is_warm()
timings['queue_wait_ms'] = max(
round((job.started_at - job.created_at).total_seconds() * 1000), 0,
) if job.started_at and job.created_at else 0
timings['asset_read_ms'] = round((perf_counter() - asset_started) * 1000)
from app.services.shipping_notice.document_layout.xfa_analysis import (
analyze_xfa_pdf,
persist_xfa_structure_analysis,
@@ -169,6 +180,11 @@ def run_shipping_document_layout(ctx: JobContext, payload: dict) -> None:
and xfa_outcome.packet_result.document_kind not in {'plain_pdf', 'acroform'}
):
snapshot = dict(xfa_outcome.snapshot)
capability = snapshot.get('xfa_capability') if isinstance(snapshot.get('xfa_capability'), dict) else {}
try:
counts['page_count'] = int(capability.get('page_count') or 0)
except (TypeError, ValueError):
counts['page_count'] = 0
persist_xfa_structure_analysis(
document_id=int(document.id),
layout_version_id=int(version.id),
@@ -212,6 +228,7 @@ def run_shipping_document_layout(ctx: JobContext, payload: dict) -> None:
document_id=document.id,
content_hash=document.content_hash,
timings=timings,
counts=counts,
)
timings['snapshot_build_ms'] = round((perf_counter() - total_started) * 1000)
if snapshot is None:
@@ -230,11 +247,14 @@ def run_shipping_document_layout(ctx: JobContext, payload: dict) -> None:
version=version,
metadata={'status': str(snapshot.get('status') or 'failed')},
)
timings['total_handler_ms'] = round((perf_counter() - total_started) * 1000)
frozen_timings, frozen_counts = freeze_layout_diagnostics(timings, counts)
finish_job(
job.id, result={
'status': final_status,
'provider_status': str(snapshot.get('status') or 'failed'),
'timings': {key: int(value) for key, value in timings.items()},
'timings': frozen_timings,
'counts': frozen_counts,
},
message=(
'文档版面解析完成' if final_status in {'ready', 'needs_review'}
@@ -244,11 +264,13 @@ def run_shipping_document_layout(ctx: JobContext, payload: dict) -> None:
from app.services.shipping_notice.order_extraction_run_service import resume_runs_waiting_for_layout
resume_runs_waiting_for_layout(document_id=int(document.id))
timings['total_handler_ms'] = round((perf_counter() - total_started) * 1000)
else:
timings['total_handler_ms'] = round((perf_counter() - total_started) * 1000)
if ctx.logger is not None:
frozen_timings, frozen_counts = freeze_layout_diagnostics(timings, counts)
ctx.logger.info(
'shipping_document_layout_timing job_id=%s version_id=%s document_id=%s timings=%s',
job.id, version.id, document.id, timings,
'shipping_document_layout_timing job_id=%s version_id=%s document_id=%s timings=%s counts=%s',
job.id, version.id, document.id, frozen_timings, frozen_counts,
)
except Exception as exc:
db.session.rollback()