feat(shipping-notice): 新增发货通知自动上传功能与候选资格评估
- 在环境配置中新增 SHIPPING_NOTICE_AUTO_UPLOAD_ENABLED 选项,默认关闭,需显式开启以支持自动上传。 - 引入新的 pytest.ini 文件,增加测试标记以支持自动化测试。 - 删除不再使用的 seed_shipping_notice_agents.py 文件,简化代码库。 - 更新相关 API,增强发货通知与候选资格评估功能,支持多种筛选条件。 - 增强文档与代码注释,反映新的业务逻辑与功能要求。
This commit is contained in:
1 parent
cef3d5b26a
commit
3a9fbac2fc
226 files changed
+25010
-2098
No files matched your search
@@ -3,8 +3,10 @@ services/shipping_notice/tool_common.py
|
||||
|
||||
用途:发货通知 Tool 链共享配置、payload 合并、消息拼接与任务事件写入(无阶段业务编排)。
|
||||
依赖:constants、models.ShippingTaskEvent、fulfillment_audit、stp_progress_contract。
|
||||
逻辑:task_workflow / inventory_workflow / fulfillment_workflow 共用;log_task_event 对
|
||||
inventory_rpa_/inventory_source_/export_/STP 事件同步投影到 ShippingAuditEvent,失败阻断业务。
|
||||
逻辑:task_workflow / inventory_workflow / fulfillment_workflow / decision_workflow 共用;
|
||||
log_task_event 对 inventory_rpa_/inventory_source_/export_/notice_replace/
|
||||
freight_cancellation_/STP 事件同步投影到 ShippingAuditEvent;
|
||||
task_lifecycle_audit_snapshot / resolve_workspace_actor_id 供旧工作台写入口补齐审计四要素。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -317,7 +319,13 @@ def log_task_event(
|
||||
from app.services.shipping_notice.stp_progress_contract import STP_AUDIT_DUAL_WRITE_PREFIXES
|
||||
|
||||
event_name = str(event_type or '')
|
||||
dual_write = event_name.startswith(('inventory_rpa_', 'inventory_source_', 'export_')) or any(
|
||||
dual_write = event_name.startswith((
|
||||
'inventory_rpa_',
|
||||
'inventory_source_',
|
||||
'export_',
|
||||
'notice_replace',
|
||||
'freight_cancellation_',
|
||||
)) or any(
|
||||
event_name.startswith(prefix) or event_name == prefix
|
||||
for prefix in STP_AUDIT_DUAL_WRITE_PREFIXES
|
||||
)
|
||||
@@ -363,6 +371,23 @@ def log_task_event(
|
||||
meta[key] = value
|
||||
elif isinstance(value, list) and all(isinstance(item, (str, int, float, bool)) or item is None for item in value):
|
||||
meta[key] = value[:32]
|
||||
# [STEP] 顶层 reason_code:优先 detail/after,其次按事件类型映射可解释性稳定码
|
||||
reason_from_detail = str(detail_obj.get('reason_code') or '').strip()
|
||||
reason_from_after = ''
|
||||
if isinstance(after, dict):
|
||||
reason_from_after = str(after.get('reason_code') or '').strip()
|
||||
reason_code = (
|
||||
reason_from_detail
|
||||
or reason_from_after
|
||||
or _audit_reason_code_for_task_event(event_name, detail_obj)
|
||||
)
|
||||
if reason_code:
|
||||
meta.setdefault('reason_code', reason_code)
|
||||
is_failure = (
|
||||
event_name.endswith('_failed')
|
||||
or event_name.endswith('_exhausted')
|
||||
or event_name.endswith('_extract_failed')
|
||||
)
|
||||
record_fulfillment_audit(
|
||||
event_type=f'task_{event_type}'[:64],
|
||||
aggregate_type='shipping_task',
|
||||
@@ -371,7 +396,8 @@ def log_task_event(
|
||||
actor_type='user' if actor_id is not None else 'system',
|
||||
actor_id=actor_id,
|
||||
task_no=str(task.task_no),
|
||||
result='success',
|
||||
reason_code=reason_code,
|
||||
result='failure' if is_failure else 'success',
|
||||
before=safe_before,
|
||||
after=safe_after or {'status': getattr(task, 'process_status', None)},
|
||||
metadata=meta,
|
||||
@@ -380,6 +406,105 @@ def log_task_event(
|
||||
)
|
||||
|
||||
|
||||
def _audit_reason_code_for_task_event(event_type: str, detail: dict[str, Any]) -> str | None:
|
||||
"""
|
||||
将任务事件类型映射为可解释性稳定 reason_code,供审计中心筛选。
|
||||
|
||||
Args:
|
||||
event_type: ShippingTaskEvent.event_type。
|
||||
detail: 事件明细;可含 error / error_code。
|
||||
|
||||
Returns:
|
||||
稳定原因码;无法映射时 None。
|
||||
"""
|
||||
name = str(event_type or '').strip()
|
||||
err_blob = f"{detail.get('error') or ''} {detail.get('error_code') or ''}".lower()
|
||||
mapping = {
|
||||
'inventory_rpa_queued': 'inventory_rpa_queued',
|
||||
'inventory_rpa_started': 'inventory_rpa_queued',
|
||||
'inventory_rpa_timeout': 'inventory_rpa_timeout',
|
||||
'inventory_rpa_exhausted': 'inventory_rpa_exhausted',
|
||||
'inventory_rpa_failed': 'inventory_rpa_export_failed',
|
||||
'notice_replaced': 'attachment_replaced',
|
||||
'notice_replace_extract_failed': 'document_text_unavailable',
|
||||
'freight_cancellation_completed': 'task_voided_for_correction',
|
||||
'task_pending_void': 'task_cancelled_by_user',
|
||||
'matched_multi_file_conflict': 'inventory_multi_file_conflict',
|
||||
'matched_shortage': 'inventory_shortage_review_required',
|
||||
'rpa_callback_rejected': 'late_callback_ignored',
|
||||
'late_callback_ignored': 'late_callback_ignored',
|
||||
}
|
||||
if name in mapping:
|
||||
code = mapping[name]
|
||||
if name == 'inventory_rpa_failed':
|
||||
if 'timeout' in err_blob:
|
||||
return 'inventory_rpa_timeout'
|
||||
if 'login' in err_blob or 'auth' in err_blob:
|
||||
return 'inventory_rpa_login_failed'
|
||||
if 'export' in err_blob:
|
||||
return 'inventory_rpa_export_failed'
|
||||
return 'inventory_rpa_export_failed'
|
||||
if name == 'notice_replace_extract_failed':
|
||||
from app.services.shipping_notice.error_explainability_contract import (
|
||||
primary_attachment_reason_from_warnings,
|
||||
)
|
||||
from_warnings = primary_attachment_reason_from_warnings(
|
||||
list(detail.get('warnings') or []),
|
||||
)
|
||||
return from_warnings or code
|
||||
return code
|
||||
if name.startswith('inventory_rpa_') and name.endswith('_failed'):
|
||||
return 'inventory_rpa_export_failed'
|
||||
if name.startswith('upload_rpa') and 'fail' in name:
|
||||
return 'upload_rpa_failed'
|
||||
return None
|
||||
|
||||
|
||||
def task_lifecycle_audit_snapshot(task: ShippingTask) -> dict[str, Any]:
|
||||
"""
|
||||
投影任务生命周期白名单快照,供 ShippingTaskEvent / ShippingAuditEvent 前后对比。
|
||||
|
||||
核心逻辑:仅输出审计允许的 status/phase/reason_code;业务明细放 detail/metadata。
|
||||
|
||||
Args:
|
||||
task: 当前 ShippingTask。
|
||||
|
||||
Returns:
|
||||
可写入 before/after 的受控快照 dict(可能为空)。
|
||||
"""
|
||||
snapshot = {
|
||||
'status': getattr(task, 'process_status', None),
|
||||
'phase': getattr(task, 'current_step', None) or getattr(task, 'review_status', None),
|
||||
'reason_code': getattr(task, 'pending_human_reason', None),
|
||||
}
|
||||
return {key: value for key, value in snapshot.items() if value is not None}
|
||||
|
||||
|
||||
def resolve_workspace_actor_id(payload: dict[str, Any] | None) -> int | None:
|
||||
"""
|
||||
从 Tool/工作台 payload 解析操作人用户 ID。
|
||||
|
||||
核心逻辑:优先 operator_id / actor_id / _trusted_user_id / user_id。
|
||||
|
||||
Args:
|
||||
payload: 路由或 Tool 入参。
|
||||
|
||||
Returns:
|
||||
用户主键,无法解析时 None。
|
||||
"""
|
||||
if not isinstance(payload, dict):
|
||||
return None
|
||||
for key in ('operator_id', 'actor_id', '_trusted_user_id', 'user_id'):
|
||||
raw = payload.get(key)
|
||||
if raw is None or raw == '':
|
||||
continue
|
||||
try:
|
||||
return int(raw)
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def log_task_action_event(
|
||||
task: ShippingTask,
|
||||
event_type: str,
|
||||
@@ -388,11 +513,25 @@ def log_task_action_event(
|
||||
source: str,
|
||||
comment: str | None = None,
|
||||
extra: dict[str, Any] | None = None,
|
||||
actor_id: int | None = None,
|
||||
before: dict[str, Any] | None = None,
|
||||
after: dict[str, Any] | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
写入标准化任务动作事件。
|
||||
|
||||
核心逻辑:在原始 event_type 之外,统一补足 reference_context 所需的动作元数据,避免各入口散落拼接。
|
||||
核心逻辑:在原始 event_type 之外,统一补足 reference_context 所需的动作元数据,
|
||||
并透传操作人与前后快照,避免各入口散落拼接。
|
||||
|
||||
Args:
|
||||
task: 目标任务。
|
||||
event_type: ShippingTaskEvent 事件码。
|
||||
action: 标准化动作名(供 reference_context)。
|
||||
source: 来源,如 workspace_api。
|
||||
comment: 可选备注。
|
||||
extra: 额外 detail 字段。
|
||||
actor_id: 操作人用户 ID;系统动作可空。
|
||||
before/after: 可选生命周期快照。
|
||||
"""
|
||||
log_task_event(
|
||||
task,
|
||||
@@ -404,6 +543,10 @@ def log_task_action_event(
|
||||
comment=comment,
|
||||
extra=extra,
|
||||
),
|
||||
actor_id=actor_id,
|
||||
source=source,
|
||||
before=before,
|
||||
after=after,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user