feat(shipping-notice): 落地 XFA 拆分、版面签名、统一进度与处理域履约

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-08-06 10:18:37 +08:00
1 parent 3a9fbac2fc
commit d2b3d74def
571 files changed
+78064 -24681

No files matched your search

@@ -6,7 +6,7 @@ services/shipping_notice/tool_common.py
逻辑: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 供旧工作台写入口补齐审计四要素。
另提供 generic/XFA/LLM 等主链灰度开关读取。
"""
from __future__ import annotations
@@ -18,7 +18,8 @@ import uuid
from flask import current_app
from app import db
from app.models import ShippingBatch, ShippingTask, ShippingTaskEvent
from app.models import ShippingNotice, ShippingTaskEvent
from app.services.shipping_notice.batch_legacy_removed import raise_shipping_batches_removed
from app.services.shipping_notice.constants import AUTO_CHAIN_FALLBACK
@@ -102,6 +103,80 @@ def profile_auto_selection_enabled() -> bool:
return cfg_bool("SHIPPING_NOTICE_PROFILE_AUTO_SELECTION_ENABLED", True)
def profile_layout_signature_required() -> bool:
"""
新 Run 候选是否强制要求有效版面签名。
阶段四全量切换前保持 False,避免未重签模板全部 fail-closed;
全部模板重新签名并发布后置 True。
"""
return cfg_bool("SHIPPING_NOTICE_PROFILE_LAYOUT_SIGNATURE_REQUIRED", False)
def xfa_enabled() -> bool:
"""是否启用 XFA 能力探测与结构分析;识别到 xfa_hybrid 后正式提取默认开启。"""
return cfg_bool("SHIPPING_NOTICE_XFA_ENABLED", True)
def xfa_shadow_mode_enabled() -> bool:
"""
XFA shadow:完整执行解析与资格评估,但必须阻断真实自动建单。
核心逻辑:开启时仍计算 shadow_would_auto_create;真实建单仍走统一 Schema/CQ
`auto_create_enabled`,本开关不再另设独立 XFA 自动建单环境变量。
"""
return cfg_bool("SHIPPING_NOTICE_XFA_SHADOW_MODE", False)
def _parse_csv_allowlist(raw: str | None) -> list[str]:
"""解析逗号/分号分隔白名单,去空并保持稳定顺序。"""
text = str(raw or '').strip()
if not text:
return []
items: list[str] = []
for part in text.replace(';', ',').split(','):
value = str(part or '').strip()
if value:
items.append(value)
return items
def xfa_customer_allowlist() -> list[int]:
"""
XFA 客户白名单(自动建单硬门禁)。
Returns:
客户主键列表;空列表表示未配置,fail-closed。
"""
raw = current_app.config.get('SHIPPING_NOTICE_XFA_CUSTOMER_ALLOWLIST')
result: list[int] = []
for item in _parse_csv_allowlist(str(raw or '')):
try:
result.append(int(item))
except (TypeError, ValueError):
continue
return sorted(set(result))
def xfa_profile_checksum_allowlist() -> list[str]:
"""XFA Profile checksum 白名单(自动建单硬门禁);空列表 fail-closed。"""
raw = current_app.config.get('SHIPPING_NOTICE_XFA_PROFILE_CHECKSUM_ALLOWLIST')
return sorted(set(_parse_csv_allowlist(str(raw or ''))))
def xfa_scenario_allowlist() -> list[str]:
"""XFA 业务场景白名单(自动建单硬门禁);默认 shipping_notice_order。"""
raw = current_app.config.get('SHIPPING_NOTICE_XFA_SCENARIO_ALLOWLIST')
if raw is None or str(raw).strip() == '':
raw = 'shipping_notice_order'
items = [
str(item).strip().lower()
for item in _parse_csv_allowlist(str(raw))
if str(item).strip()
]
return sorted(set(items)) or ['shipping_notice_order']
def llm_review_enabled() -> bool:
"""确定性解析失败后是否允许请求 Dify 最小证据建议(不得自动冻结)。"""
return cfg_bool("SHIPPING_NOTICE_LLM_REVIEW_ENABLED", False)
@@ -135,7 +210,7 @@ def payload_wants_auto_chain_fallback(payload: dict[str, Any] | None) -> bool:
def merge_tool_payload(
task: ShippingTask,
task: ShippingNotice,
payload: dict[str, Any] | None = None,
*,
extra: dict[str, Any] | None = None,
@@ -149,7 +224,7 @@ def merge_tool_payload(
return result
def tool_payload_for_task(task: ShippingTask, payload: dict[str, Any] | None = None) -> dict[str, Any]:
def tool_payload_for_task(task: ShippingNotice, payload: dict[str, Any] | None = None) -> dict[str, Any]:
"""从 task 与原始 payload 提取 workflow 间传递的最小字段集(task_id + lang)。"""
result: dict[str, Any] = {"task_id": task.task_no}
if not payload:
@@ -172,7 +247,7 @@ def prepend_tool_message(result: dict[str, Any], prefix: str) -> dict[str, Any]:
def set_task_step_progress(
task: ShippingTask,
task: ShippingNotice,
*,
step_key: str,
step_title: str,
@@ -207,7 +282,7 @@ def set_task_step_progress(
task.step_payload_json = payload
def clear_task_step_progress(task: ShippingTask) -> None:
def clear_task_step_progress(task: ShippingNotice) -> None:
"""清空任务步骤内进度,保留其他 step_payload_json 字段。"""
payload = dict(task.step_payload_json or {})
payload.pop("current_step_progress", None)
@@ -229,14 +304,14 @@ def _clean_event_value(value: Any) -> str:
return str(value or "").strip()
def _task_batch_no(task: ShippingTask | None) -> str:
def _task_batch_no(task: ShippingNotice | None) -> str:
"""从任务关系中提取批次号。"""
batch = getattr(task, "batch", None)
return _clean_event_value(getattr(batch, "batch_no", None))
def build_task_action_event_detail(
task: ShippingTask,
task: ShippingNotice,
*,
action: str,
source: str,
@@ -277,7 +352,7 @@ def build_task_action_event_detail(
def log_task_event(
task: ShippingTask,
task: ShippingNotice,
event_type: str,
detail: dict[str, Any] | None = None,
*,
@@ -460,14 +535,14 @@ def _audit_reason_code_for_task_event(event_type: str, detail: dict[str, Any]) -
return None
def task_lifecycle_audit_snapshot(task: ShippingTask) -> dict[str, Any]:
def task_lifecycle_audit_snapshot(task: ShippingNotice) -> dict[str, Any]:
"""
投影任务生命周期白名单快照,供 ShippingTaskEvent / ShippingAuditEvent 前后对比。
核心逻辑:仅输出审计允许的 status/phase/reason_code;业务明细放 detail/metadata。
Args:
task: 当前 ShippingTask。
task: 当前 ShippingNotice。
Returns:
可写入 before/after 的受控快照 dict(可能为空)。
@@ -506,7 +581,7 @@ def resolve_workspace_actor_id(payload: dict[str, Any] | None) -> int | None:
def log_task_action_event(
task: ShippingTask,
task: ShippingNotice,
event_type: str,
*,
action: str,
@@ -551,8 +626,8 @@ def log_task_action_event(
def switch_batch_focus(
batch: ShippingBatch,
task: ShippingTask,
batch: Any,
task: ShippingNotice,
*,
source: str,
event_type: str = "focus_switched",
@@ -563,39 +638,8 @@ def switch_batch_focus(
"""
切换批次焦点任务并写入标准事件。
核心逻辑:统一处理 current_focus_task_no 更新与 focus 切换事件,供 workspace/tool/orchestrator 复用。
Args:
batch: 所属批次。
task: 新焦点任务。
source: 触发来源,如 workspace_api/tool_api/backend_orchestrator。
event_type: 事件名;用户态默认 focus_switched,系统自动切票可传 focus_auto_switched。
comment: 用户备注。
emit_when_unchanged: 焦点未变化时是否仍记事件。
extra: 附加字段。
Returns:
是否实际发生焦点变化。
Raises:
ValidationError: shipping_batches 表已物理删除(co8e9f0a1b2c)。
"""
previous_focus_task_no = _clean_event_value(getattr(batch, "current_focus_task_no", None))
next_focus_task_no = _clean_event_value(getattr(task, "task_no", None))
changed = previous_focus_task_no != next_focus_task_no
batch.current_focus_task_no = next_focus_task_no or None
if changed or emit_when_unchanged:
detail_extra = {
"previous_focus_task_no": previous_focus_task_no or None,
"next_focus_task_no": next_focus_task_no or None,
"changed": changed,
}
if extra:
detail_extra.update(extra)
log_task_action_event(
task,
event_type,
action="switch_focus",
source=source,
comment=comment,
extra=detail_extra,
)
return changed
del batch, task, source, event_type, comment, emit_when_unchanged, extra
raise_shipping_batches_removed()