fix(chat-input-bar,shipping-process-gate): 修复库存RPA重试按钮隐藏问题
- 重构发货通知流程的Gate投影逻辑,统一库存RPA失败后的恢复动作,避免被降级为人工上传入口 - 新增全覆盖单元测试验证恢复动作逻辑
This commit is contained in:
1 parent
3c0e08377c
commit
eee9163ce0
12 files changed
+350
-78
No files matched your search
@@ -421,6 +421,16 @@ def is_business_user_action(action_code: str | None) -> bool:
|
||||
return bool(code) and code in PROCESS_GATE_BUSINESS_USER_ACTION_CODES
|
||||
|
||||
|
||||
def inventory_preparation_primary_action(status: str | None) -> str | None:
|
||||
"""裁决库存准备 Gate 面向业务用户的主动作。"""
|
||||
normalized_status = str(status or '').strip().lower()
|
||||
if normalized_status == 'waiting_external':
|
||||
return 'poll_inventory_preparation'
|
||||
if normalized_status in {'blocked_manual', 'blocked_retryable'}:
|
||||
return 'retry_inventory_preparation'
|
||||
return None
|
||||
|
||||
|
||||
def is_technical_gate_party(responsible_party: str | None) -> bool:
|
||||
"""判断责任方是否为技术自动 Gate(system/worker)。"""
|
||||
return str(responsible_party or '').strip() in PROCESS_GATE_TECHNICAL_PARTIES
|
||||
|
||||
@@ -779,7 +779,11 @@ def _handle_retry_inventory_preparation(
|
||||
operation_id=str(payload.get('operation_id') or '').strip() or None,
|
||||
gate_attempt_id=int(payload.get('attempt_id')) if payload.get('attempt_id') else None,
|
||||
)
|
||||
response = ensure_inventory(_task_payload(execution, payload))
|
||||
inventory_payload = _task_payload(execution, payload)
|
||||
# 用户重试必须进入逐源 RPA Attempt,生成新的 inventory trigger;轮询动作继续复用普通模式。
|
||||
if payload.get('action_code') != 'poll_inventory_preparation':
|
||||
inventory_payload['mode'] = 'retry_rpa'
|
||||
response = ensure_inventory(inventory_payload)
|
||||
if preparation_round is not None:
|
||||
from app.services.shipping_notice.inventory_recovery_service import sync_inventory_preparation_round
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ from app.services.shipping_notice.process_gate_contract import (
|
||||
PROCESS_GATE_RECOVERABLE_STATUSES,
|
||||
action_presentation,
|
||||
get_process_gate_definition,
|
||||
inventory_preparation_primary_action,
|
||||
is_business_user_action,
|
||||
)
|
||||
from app.services.shipping_notice.process_gate_error_sanitizer import sanitize_gate_error_text
|
||||
@@ -83,6 +84,87 @@ def _projection_primary_next_action(*, gate_code: str, status: str) -> str | Non
|
||||
return str(value) if value else None
|
||||
|
||||
|
||||
# 库存 RPA / 准备失败原因:统一由业务用户触发「重新获取库存」(retry_inventory_preparation)。
|
||||
# 与 error_explainability_contract.EXPLAINABILITY_REASON_META 中 inventory_file_empty 等
|
||||
# 的 prescribed action 保持一致;这些原因在 process_gate_contract 中分类为 retryable_tech,
|
||||
# 但因 inventory_preparation 的 responsible_party='worker',历史投影会按「worker/retry_ 技术
|
||||
# 动作屏蔽」规则将其从业务按钮隐藏,导致用户只能看到被降级的人工上传入口。
|
||||
def _resolve_recovery_next_action(
|
||||
*, blocking: dict[str, Any] | None, full: dict[str, Any], execution: Any,
|
||||
) -> tuple[str | None, bool]:
|
||||
"""
|
||||
单一裁决:由阻断原因码 / Gate 状态推导业务用户主恢复动作。
|
||||
|
||||
[STEP 1] 库存 RPA 失败(inventory_preparation Gate + retryable_tech 原因码)统一下发
|
||||
``retry_inventory_preparation``(重新获取库存),绕过历史「worker/retry_ 技术动作屏蔽」
|
||||
分支,使业务用户在库存 RPA 失败时可直接重试,而非被降级为人工上传入口。
|
||||
[STEP 2] 货代上传 RPA / 人工交接失败保持统一 RPA 重试入口。
|
||||
[STEP 3] 其余沿用 Gate 合同默认 next_action(primary 或按 status 推导),并保留
|
||||
RecoveryPlan 复核入口兜底;技术责任方的 retry_* 仍默认仅作系统执行动作。
|
||||
"""
|
||||
if not blocking:
|
||||
return None, False
|
||||
blocking_gate_code = str((blocking or {}).get('gate_code') or '').strip().lower()
|
||||
normalized_reason = str((blocking or {}).get('blocking_reason_code') or '').strip().lower()
|
||||
status = str((blocking or {}).get('status') or '').strip().lower()
|
||||
|
||||
# [STEP 1] 库存准备失败统一允许业务用户手动重新获取库存;人工上传是辅助输入入口。
|
||||
if blocking_gate_code == 'inventory_preparation':
|
||||
inventory_action = inventory_preparation_primary_action(status)
|
||||
if inventory_action:
|
||||
return inventory_action, False
|
||||
|
||||
# [STEP 2] 货代上传 RPA 失败 / 人工交接失败:保持统一的 RPA 重试入口。
|
||||
if (
|
||||
blocking_gate_code == 'freight_upload_rpa'
|
||||
and normalized_reason in {'upload_rpa_failed', 'upload_rpa_login_failed', 'upload_platform_rejected'}
|
||||
):
|
||||
return 'retry_freight_upload_rpa', False
|
||||
if (
|
||||
blocking_gate_code == 'manual_upload_handoff'
|
||||
and normalized_reason in {'manual_upload_pending', 'upload_result_unknown'}
|
||||
):
|
||||
return 'retry_freight_upload_rpa', False
|
||||
|
||||
# [STEP 3] 其余沿用 Gate 合同默认 next_action(primary 或按 status 推导)。
|
||||
next_action = full.get('primary_next_action_code') or _projection_primary_next_action(
|
||||
gate_code=blocking_gate_code, status=status,
|
||||
)
|
||||
technical_auto_only = False
|
||||
# 技术责任方的 retry_* 只作为系统执行动作,不能直接充当业务用户主按钮;
|
||||
# RecoveryPlan 仍可在下方投影为经过权限校验的恢复入口。
|
||||
if next_action and (
|
||||
not is_business_user_action(str(next_action))
|
||||
or (
|
||||
str(full.get('responsible_party') or '').strip().lower() in {'system', 'worker'}
|
||||
and str(next_action).startswith('retry_')
|
||||
)
|
||||
):
|
||||
next_action = None
|
||||
technical_auto_only = True
|
||||
|
||||
# RecoveryPlan 复核入口兜底(inventory_matching / inventory_sufficiency)。
|
||||
if not next_action:
|
||||
recovery_targets = full.get('recovery_targets') or []
|
||||
if recovery_targets and blocking_gate_code == 'inventory_matching':
|
||||
next_action = 'open_inventory_match_review'
|
||||
technical_auto_only = False
|
||||
elif recovery_targets and blocking_gate_code == 'inventory_sufficiency':
|
||||
next_action = 'open_shortage_review'
|
||||
technical_auto_only = False
|
||||
# 库存准备/匹配/货代上传 RPA 在 blocked_retryable 时仍允许业务用户恢复。
|
||||
if not next_action and blocking_gate_code in {
|
||||
'inventory_preparation', 'inventory_matching', 'freight_upload_rpa',
|
||||
}:
|
||||
candidate = _projection_primary_next_action(
|
||||
gate_code=blocking_gate_code, status=status,
|
||||
)
|
||||
if candidate and is_business_user_action(candidate):
|
||||
next_action = candidate
|
||||
technical_auto_only = False
|
||||
return next_action, technical_auto_only
|
||||
|
||||
|
||||
def serialize_gate_attempt(
|
||||
attempt: ShippingProcessGateAttempt | None,
|
||||
*,
|
||||
@@ -430,80 +512,13 @@ def project_user_gate_summary(execution: ShippingProcessExecution) -> dict[str,
|
||||
if diagnostic_hint:
|
||||
break
|
||||
reason_code = (blocking or {}).get('blocking_reason_code')
|
||||
next_action = full.get('primary_next_action_code') or (
|
||||
_projection_primary_next_action(
|
||||
gate_code=str((blocking or {}).get('gate_code') or ''),
|
||||
status=str((blocking or {}).get('status') or ''),
|
||||
) if blocking else None
|
||||
# [STEP] 单一裁决:由阻断原因码 / Gate 状态推导业务用户主恢复动作。
|
||||
# 库存 RPA 失败统一下发「重新获取库存」,不再因 worker 责任方被技术动作屏蔽。
|
||||
next_action, technical_auto_only = _resolve_recovery_next_action(
|
||||
blocking=blocking,
|
||||
full=full,
|
||||
execution=execution,
|
||||
)
|
||||
# [STEP] 兼容历史 Gate 行:RPA 失败曾被旧契约错误写成
|
||||
# regenerate_manual_upload_todo;当前 Gate + 原因码才是履约真相,
|
||||
# 统一纠正为用户可主动执行的 RPA 重试。AUTO_UPLOAD 只控制自动触发,
|
||||
# 不参与手动动作投影。
|
||||
blocking_gate_code = str((blocking or {}).get('gate_code') or '').strip().lower()
|
||||
normalized_reason = str(reason_code or '').strip().lower()
|
||||
if (
|
||||
blocking_gate_code == 'freight_upload_rpa'
|
||||
and normalized_reason in {
|
||||
'upload_rpa_failed',
|
||||
'upload_rpa_login_failed',
|
||||
'upload_platform_rejected',
|
||||
}
|
||||
):
|
||||
next_action = 'retry_freight_upload_rpa'
|
||||
elif (
|
||||
blocking_gate_code == 'manual_upload_handoff'
|
||||
and normalized_reason in {'manual_upload_pending', 'upload_result_unknown'}
|
||||
):
|
||||
# 人工交接仍保留人工登记入口(Footer),同时在 Gate 卡提供显式 RPA 重试。
|
||||
next_action = 'retry_freight_upload_rpa'
|
||||
# [STEP] 所有冻结 Gate 动作均可投影到工作台;是否可执行由页面 canHandle
|
||||
# 与 Workspace API 的权限/版本校验共同决定,禁止前端自行派生动作。
|
||||
technical_auto_only = False
|
||||
# 技术责任方的 retry_* 只作为系统执行动作,不能直接充当业务用户主按钮;
|
||||
# RecoveryPlan 仍可在下方投影为经过权限校验的恢复入口。
|
||||
if (
|
||||
next_action
|
||||
and (
|
||||
not is_business_user_action(str(next_action))
|
||||
or (
|
||||
str(full.get('responsible_party') or '').strip().lower() in {'system', 'worker'}
|
||||
and str(next_action).startswith('retry_')
|
||||
)
|
||||
)
|
||||
):
|
||||
next_action = None
|
||||
technical_auto_only = bool(blocking)
|
||||
|
||||
# [STEP] 正式 OT 后的库存阻断仍需给业务用户提供精确入口:技术主动作
|
||||
# (retry_inventory_*)不直接暴露,但 RecoveryPlan 存在时必须保留当前
|
||||
# Gate 的复核入口,否则 apply_post_ot_gate_rules 会把整个阻断误判为系统态,
|
||||
# 前端也就不会挂载 ProcessGateRecoveryCard。
|
||||
if blocking and not next_action:
|
||||
gate_code = str(blocking.get('gate_code') or '').strip().lower()
|
||||
recovery_targets = full.get('recovery_targets') or []
|
||||
if recovery_targets and gate_code == 'inventory_matching':
|
||||
next_action = 'open_inventory_match_review'
|
||||
technical_auto_only = False
|
||||
elif recovery_targets and gate_code == 'inventory_sufficiency':
|
||||
next_action = 'open_shortage_review'
|
||||
technical_auto_only = False
|
||||
|
||||
# 正式 OT 后仍需允许业务用户恢复履约链路中的技术失败:
|
||||
# 库存准备/匹配以及货代上传 RPA 的动作码由 Gate 合同固定,
|
||||
# 不把其他 system/worker 节点泛化为可人工操作。
|
||||
if not next_action and gate_code in {
|
||||
'inventory_preparation',
|
||||
'inventory_matching',
|
||||
'freight_upload_rpa',
|
||||
}:
|
||||
candidate = _projection_primary_next_action(
|
||||
gate_code=gate_code,
|
||||
status=str(blocking.get('status') or ''),
|
||||
)
|
||||
if candidate and is_business_user_action(candidate):
|
||||
next_action = candidate
|
||||
technical_auto_only = False
|
||||
# 无当前阻断时禁止残留 confirm_customer,避免「客户已确认」仍显示为优先处理。
|
||||
if not blocking and str(next_action or '').strip() == 'confirm_customer':
|
||||
next_action = None
|
||||
@@ -513,6 +528,11 @@ def project_user_gate_summary(execution: ShippingProcessExecution) -> dict[str,
|
||||
if blocking:
|
||||
if next_action and is_business_user_action(str(next_action)):
|
||||
allowed_actions.append(str(next_action))
|
||||
if (
|
||||
str(blocking.get('gate_code') or '').strip().lower() == 'inventory_preparation'
|
||||
and next_action == 'retry_inventory_preparation'
|
||||
):
|
||||
allowed_actions.append('upload_inventory_file')
|
||||
for target in full.get('recovery_targets') or []:
|
||||
code = str(target.get('action_code') or '').strip()
|
||||
if code and code not in allowed_actions and is_business_user_action(code):
|
||||
|
||||
@@ -20,6 +20,7 @@ from app.services.shipping_notice.fulfillment_audit import record_fulfillment_au
|
||||
from app.services.shipping_notice.process_gate_contract import (
|
||||
PROCESS_GATE_ACTION_CODES,
|
||||
get_process_gate_definition,
|
||||
inventory_preparation_primary_action,
|
||||
is_business_user_action,
|
||||
is_technical_gate_party,
|
||||
primary_next_action_for_gate,
|
||||
@@ -217,7 +218,8 @@ def request_gate_action(
|
||||
details={'gate_code': gate.gate_code, 'action_code': action},
|
||||
)
|
||||
|
||||
primary = gate.next_action_code or primary_next_action_for_gate(
|
||||
primary = inventory_preparation_primary_action(gate.status) if gate.gate_code == 'inventory_preparation' else None
|
||||
primary = primary or gate.next_action_code or primary_next_action_for_gate(
|
||||
gate_code=gate.gate_code, status=gate.status,
|
||||
)
|
||||
# [STEP] 兼容历史 Gate 20:用户投影已将人工待办旧动作纠正为显式 RPA
|
||||
@@ -236,6 +238,8 @@ def request_gate_action(
|
||||
'adopt_rule_suggestion',
|
||||
'keep_manual_inventory_decision',
|
||||
}
|
||||
if gate.gate_code == 'inventory_preparation' and primary == 'retry_inventory_preparation':
|
||||
allowed_side.add('upload_inventory_file')
|
||||
if primary and action != primary and action not in allowed_side and recovery_definition is None:
|
||||
raise ValidationError(
|
||||
'Action is not the primary recovery action for this gate',
|
||||
|
||||
@@ -10,6 +10,9 @@ services/shipping_notice/ui_state.py
|
||||
stp_facts、progress_nodes);
|
||||
若存在 ProcessExecution:阻断时清空 action_buttons;非阻断时仅保留
|
||||
download_excel / mark_manual_uploaded / refresh(恢复动作仅由 process_gate 驱动);
|
||||
[兼容] action_buttons 为只读兼容字段:权威可恢复操作入口已统一为
|
||||
process_gate.next_action_code(如 retry_inventory_preparation),本字段仅保留供
|
||||
任务详情 / 聊天状态 / 旧入口消费,禁止再向其新增恢复动作;待确认无活跃消费者后删除。
|
||||
Task 即正式 OT,Gate 摘要必须套用 OT 后规则(系统等待不冒充人工待办);
|
||||
批次 UI 仅作会话上传集合摘要,不作为履约写操作面。
|
||||
"""
|
||||
@@ -631,6 +634,8 @@ def build_task_ui_state(task: ShippingNotice) -> dict[str, Any]:
|
||||
"current_step": current_step,
|
||||
"steps": steps,
|
||||
"summary": summary,
|
||||
# [兼容] action_buttons 为只读兼容字段(见模块 docstring);新 UI 应消费
|
||||
# summary.process_gate.next_action_code,本字段仅作历史消费者过渡,不再扩展。
|
||||
"action_buttons": action_buttons,
|
||||
"process_gate": process_gate,
|
||||
"updated_at": task.updated_at.isoformat() if task.updated_at else None,
|
||||
|
||||
Reference in new issue
Block a user