feat(shipping-notice): 收敛复核状态隔离、STP 重试幂等与 XFA 合成预览标注
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
84a881b554
commit
74270adc22
149 files changed
+20111
-3871
No files matched your search
@@ -0,0 +1,199 @@
|
||||
"""
|
||||
services/shipping_notice/processing_ownership.py
|
||||
|
||||
用途:订单处理投影不一致审计与正式 Notice 任务归属校验(fail-closed)。
|
||||
依赖:audit_service.append_shipping_audit_event、processing_projection_invariants 错误码、
|
||||
models ShippingOrderTask / ShippingOrderReviewCase、AppError ConflictError、logging。
|
||||
逻辑:投影组装后写 inconsistent 审计;履约写 API / service 在执行前校验 Notice↔OT/Case/Run。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from app.core.errors import ConflictError
|
||||
from app.extensions import db
|
||||
from app.models.shipping import ShippingNotice
|
||||
from app.models.shipping_order_review import ShippingOrderReviewCase
|
||||
from app.models.shipping_order_task import ShippingOrderTask
|
||||
from app.services.shipping_notice.audit_service import append_shipping_audit_event
|
||||
from app.services.shipping_notice.processing_projection_invariants import (
|
||||
PROJECTION_INCONSISTENT_ERROR_CODE,
|
||||
TASK_OWNERSHIP_MISMATCH_ERROR_CODE,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def record_projection_inconsistency_audit(payload: dict[str, Any]) -> None:
|
||||
"""
|
||||
当投影带有 inconsistency 码时写入 progress_audit 结构化事件。
|
||||
|
||||
核心逻辑:不阻断读路径;失败仅打 logger,避免拖垮投影 GET。
|
||||
|
||||
Args:
|
||||
payload: reconcile 后的 ShippingProcessingProjection dict。
|
||||
"""
|
||||
code = str(payload.get('projection_inconsistency_code') or '').strip()
|
||||
if code != PROJECTION_INCONSISTENT_ERROR_CODE:
|
||||
return
|
||||
work_item_no = str(payload.get('work_item_no') or '').strip() or None
|
||||
notice_no = str(payload.get('shipping_notice_no') or '').strip() or None
|
||||
stage = str(payload.get('stage') or '').strip() or None
|
||||
try:
|
||||
append_shipping_audit_event(
|
||||
event_type='shipping_processing_projection_inconsistent',
|
||||
aggregate_type='order_task',
|
||||
aggregate_id=work_item_no or notice_no or 'unknown',
|
||||
task_no=notice_no,
|
||||
run_no=str(payload.get('run_no') or '').strip() or None,
|
||||
review_case_no=str(payload.get('case_no') or '').strip() or None,
|
||||
conversation_id=str(payload.get('conversation_id') or '').strip() or None,
|
||||
actor_type='system',
|
||||
result='blocked',
|
||||
error_code=PROJECTION_INCONSISTENT_ERROR_CODE,
|
||||
reason_code=PROJECTION_INCONSISTENT_ERROR_CODE,
|
||||
reason='shipping_notice_no present with pre-create stage',
|
||||
metadata={
|
||||
'work_item_no': work_item_no,
|
||||
'shipping_notice_no': notice_no,
|
||||
'stage': stage,
|
||||
'status': str(payload.get('status') or '').strip() or None,
|
||||
},
|
||||
persist_snapshots=False,
|
||||
)
|
||||
db.session.flush()
|
||||
except Exception: # noqa: BLE001 — 读路径不得因审计失败 500
|
||||
_LOGGER.warning(
|
||||
'failed to persist projection inconsistency audit code=%s work_item_no=%s',
|
||||
PROJECTION_INCONSISTENT_ERROR_CODE,
|
||||
work_item_no,
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
|
||||
def assert_notice_task_ownership(
|
||||
task: ShippingNotice,
|
||||
*,
|
||||
expected_order_task_no: str | None = None,
|
||||
expected_review_case_no: str | None = None,
|
||||
expected_run_no: str | None = None,
|
||||
actor_id: str | int | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
校验正式 Notice 与当前 Case/Run/OT 归属一致;不一致则抛 AppError。
|
||||
|
||||
核心逻辑:调用方传入期望 OT/Case/Run 时,与 Notice.order_task_id 链路比对;
|
||||
任一不匹配 → ConflictError(SHIPPING_PROCESSING_TASK_OWNERSHIP_MISMATCH) + 审计。
|
||||
|
||||
Args:
|
||||
task: 已加载的 ShippingNotice。
|
||||
expected_order_task_no: 工作台权威 OT 编号。
|
||||
expected_review_case_no: 当前 Review Case 编号。
|
||||
expected_run_no: 当前 Extraction Run 编号。
|
||||
actor_id: 操作者 id(审计)。
|
||||
|
||||
Raises:
|
||||
ConflictError: 归属不匹配。
|
||||
"""
|
||||
expect_ot = str(expected_order_task_no or '').strip() or None
|
||||
expect_case = str(expected_review_case_no or '').strip() or None
|
||||
expect_run = str(expected_run_no or '').strip() or None
|
||||
if not expect_ot and not expect_case and not expect_run:
|
||||
return
|
||||
|
||||
order_task: ShippingOrderTask | None = None
|
||||
ot_id = getattr(task, 'order_task_id', None)
|
||||
if ot_id is not None:
|
||||
order_task = db.session.get(ShippingOrderTask, int(ot_id))
|
||||
|
||||
actual_ot = str(getattr(order_task, 'task_no', None) or '').strip() or None
|
||||
actual_run = None
|
||||
if order_task is not None and getattr(order_task, 'extraction_run_id', None):
|
||||
from app.models.shipping_order_extraction_run import ShippingOrderExtractionRun
|
||||
run = db.session.get(ShippingOrderExtractionRun, int(order_task.extraction_run_id))
|
||||
actual_run = str(getattr(run, 'run_no', None) or '').strip() or None
|
||||
|
||||
actual_case = None
|
||||
if expect_case:
|
||||
case = ShippingOrderReviewCase.query.filter_by(case_no=expect_case).first()
|
||||
if case is None:
|
||||
actual_case = None
|
||||
else:
|
||||
# Case 须指向同一 Notice 或同一 candidate/OT。
|
||||
confirmed_id = getattr(case, 'confirmed_task_id', None)
|
||||
if confirmed_id is not None and int(confirmed_id) == int(task.id):
|
||||
actual_case = expect_case
|
||||
elif order_task is not None and getattr(case, 'candidate_id', None) and getattr(order_task, 'candidate_id', None):
|
||||
if int(case.candidate_id) == int(order_task.candidate_id):
|
||||
actual_case = expect_case
|
||||
else:
|
||||
actual_case = None
|
||||
|
||||
mismatch = False
|
||||
if expect_ot and actual_ot != expect_ot:
|
||||
mismatch = True
|
||||
if expect_run and actual_run and actual_run != expect_run:
|
||||
mismatch = True
|
||||
if expect_run and not actual_run:
|
||||
mismatch = True
|
||||
if expect_case and actual_case != expect_case:
|
||||
mismatch = True
|
||||
|
||||
if not mismatch:
|
||||
return
|
||||
|
||||
_LOGGER.warning(
|
||||
'shipping_processing_task_ownership_mismatch code=%s task_no=%s '
|
||||
'expected_ot=%s actual_ot=%s expected_case=%s expected_run=%s actual_run=%s',
|
||||
TASK_OWNERSHIP_MISMATCH_ERROR_CODE,
|
||||
getattr(task, 'task_no', None),
|
||||
expect_ot,
|
||||
actual_ot,
|
||||
expect_case,
|
||||
expect_run,
|
||||
actual_run,
|
||||
)
|
||||
try:
|
||||
append_shipping_audit_event(
|
||||
event_type='shipping_processing_task_ownership_mismatch',
|
||||
aggregate_type='shipping_notice',
|
||||
aggregate_id=str(getattr(task, 'task_no', None) or task.id),
|
||||
task_no=str(getattr(task, 'task_no', None) or '').strip() or None,
|
||||
run_no=expect_run or actual_run,
|
||||
review_case_no=expect_case,
|
||||
actor_type='user' if actor_id is not None else 'system',
|
||||
actor_id=actor_id,
|
||||
result='blocked',
|
||||
error_code=TASK_OWNERSHIP_MISMATCH_ERROR_CODE,
|
||||
reason_code=TASK_OWNERSHIP_MISMATCH_ERROR_CODE,
|
||||
reason='formal notice does not belong to current case/run/order task',
|
||||
metadata={
|
||||
'expected_order_task_no': expect_ot,
|
||||
'actual_order_task_no': actual_ot,
|
||||
'expected_review_case_no': expect_case,
|
||||
'expected_run_no': expect_run,
|
||||
'actual_run_no': actual_run,
|
||||
},
|
||||
persist_snapshots=False,
|
||||
)
|
||||
db.session.flush()
|
||||
except Exception: # noqa: BLE001
|
||||
_LOGGER.warning(
|
||||
'failed to persist ownership mismatch audit task_no=%s',
|
||||
getattr(task, 'task_no', None),
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
raise ConflictError(
|
||||
message='Formal task does not match the current order processing context',
|
||||
code=TASK_OWNERSHIP_MISMATCH_ERROR_CODE,
|
||||
details={
|
||||
'expected_order_task_no': expect_ot,
|
||||
'actual_order_task_no': actual_ot,
|
||||
'expected_review_case_no': expect_case,
|
||||
'expected_run_no': expect_run,
|
||||
'actual_run_no': actual_run,
|
||||
},
|
||||
)
|
||||
Reference in new issue
Block a user