feat(shipping-process-gates): 落地全门禁中断恢复并消除双真相
纳入 Execution/Gate/Attempt 模型与迁移、Worker/API/前端恢复卡片与 Admin 诊断, Gate 阻断时清零旧 action_buttons,异常摘要统一脱敏,并为运维筛选与 Attempt Diff 补齐入口。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
b352c281d5
commit
323928626b
92 files changed
+11930
-458
No files matched your search
@@ -27,5 +27,6 @@ import app.api.admin.shipping_order_schema # noqa: F401
|
||||
import app.api.admin.shipping_field_assets # noqa: F401
|
||||
import app.api.admin.shipping_order_document_profiles # noqa: F401
|
||||
import app.api.admin.shipping_document_layouts # noqa: F401
|
||||
import app.api.admin.shipping_process_gate # noqa: F401
|
||||
|
||||
__all__ = ['admin_bp']
|
||||
@@ -0,0 +1,85 @@
|
||||
"""
|
||||
backend/app/api/admin/shipping_process_gate.py
|
||||
|
||||
用途:货代全门禁恢复 Admin API(Execution 列表/诊断、Gate Attempt 运维视图)。
|
||||
依赖:admin Blueprint、require_admin、process_gate_api_service、process_gate_recovery_service。
|
||||
逻辑:仅 shipping admin(platform admin 角色)可访问;只读诊断与 Attempt 详情,不含用户写路径。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from flask import g, request
|
||||
|
||||
from app.api.admin.blueprint import admin_bp
|
||||
from app.core.decorators import require_admin
|
||||
from app.core.errors import ValidationError
|
||||
from app.core.response import ok
|
||||
from app.services.shipping_notice.process_gate_api_service import (
|
||||
get_admin_gate_attempt,
|
||||
get_admin_process_execution,
|
||||
list_admin_gate_attempts,
|
||||
list_admin_process_executions,
|
||||
)
|
||||
from app.services.shipping_notice.process_gate_recovery_service import get_execution_by_no
|
||||
|
||||
|
||||
def _query_int(name: str, default: int) -> int:
|
||||
"""读取可选整数查询参数;非整数抛出统一参数错误。"""
|
||||
raw = request.args.get(name)
|
||||
if raw in (None, ''):
|
||||
return default
|
||||
try:
|
||||
return int(raw)
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ValidationError(f'{name} must be an integer') from exc
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/process-executions', methods=['GET'])
|
||||
@require_admin
|
||||
def admin_list_process_executions():
|
||||
"""分页返回 Process Execution 管理员摘要列表。"""
|
||||
page = _query_int('page', 1)
|
||||
page_size = _query_int('page_size', 25)
|
||||
if page < 1 or not 1 <= page_size <= 100:
|
||||
raise ValidationError('page must be positive and page_size must be between 1 and 100')
|
||||
payload = list_admin_process_executions(
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
status=request.args.get('status'),
|
||||
gate_code=request.args.get('gate_code'),
|
||||
blocking_reason_code=request.args.get('blocking_reason_code'),
|
||||
responsible_party=request.args.get('responsible_party'),
|
||||
q=request.args.get('q'),
|
||||
updated_from=request.args.get('updated_from'),
|
||||
updated_to=request.args.get('updated_to'),
|
||||
)
|
||||
return ok(payload, request_id=getattr(g, 'request_id', None))
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/process-executions/<execution_no>', methods=['GET'])
|
||||
@require_admin
|
||||
def admin_get_process_execution(execution_no: str):
|
||||
"""读取单次 Execution 的完整 Gate 诊断投影。"""
|
||||
execution = get_execution_by_no(execution_no)
|
||||
payload = get_admin_process_execution(execution)
|
||||
return ok(payload, request_id=getattr(g, 'request_id', None))
|
||||
|
||||
|
||||
@admin_bp.route(
|
||||
'/shipping-notice/process-executions/<execution_no>/gates/<gate_code>/attempts',
|
||||
methods=['GET'],
|
||||
)
|
||||
@require_admin
|
||||
def admin_list_process_gate_attempts(execution_no: str, gate_code: str):
|
||||
"""返回指定 Gate 的全部 Attempt 摘要(含 Gate 诊断投影)。"""
|
||||
execution = get_execution_by_no(execution_no)
|
||||
payload = list_admin_gate_attempts(execution, gate_code)
|
||||
return ok(payload, request_id=getattr(g, 'request_id', None))
|
||||
|
||||
|
||||
@admin_bp.route('/shipping-notice/process-gate-attempts/<int:attempt_id>', methods=['GET'])
|
||||
@require_admin
|
||||
def admin_get_process_gate_attempt(attempt_id: int):
|
||||
"""按主键读取 Attempt 详情及 Execution/Gate 上下文。"""
|
||||
payload = get_admin_gate_attempt(attempt_id)
|
||||
return ok(payload, request_id=getattr(g, 'request_id', None))
|
||||
@@ -1772,7 +1772,12 @@ def chat_completions():
|
||||
yield terminal_line
|
||||
except Exception as exc:
|
||||
current_app.logger.exception("[chat-api] relay stream failed request_id=%s session_id=%s", request_id, local_session_id)
|
||||
payload = {'event': 'error', 'message': str(exc), 'conversation_id': local_session_id, 'session_status': 'error'}
|
||||
payload = {
|
||||
'event': 'error',
|
||||
'message': '当前服务暂时无法完成回答,请稍后重试。',
|
||||
'conversation_id': local_session_id,
|
||||
'session_status': 'error',
|
||||
}
|
||||
yield f"data: {json.dumps(payload, ensure_ascii=False)}\n\n"
|
||||
for terminal_line in iter_handler_terminal_sse(
|
||||
relay_stream_ctx,
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
"""
|
||||
backend/app/api/workspace/__init__.py
|
||||
|
||||
用途:工作台域 Blueprint 聚合(schedule、shipping_notice、file_check、announcements)。
|
||||
用途:工作台域 Blueprint 聚合(schedule、shipping_notice、file_check、announcements、process_gate 路由)。
|
||||
依赖:workspace 各子模块 blueprint。
|
||||
逻辑:import 子模块注册路由;register_blueprints 从本包导入各 bp。
|
||||
逻辑:import 子模块注册路由;register_blueprints 从本包导入各 bp;process_gate 路由挂载于 workspace_shipping_notice_bp。
|
||||
"""
|
||||
from app.api.workspace.announcements import workspace_announcements_bp
|
||||
from app.api.workspace.file_check import workspace_file_check_bp
|
||||
from app.api.workspace.schedule import workspace_schedule_bp
|
||||
from app.api.workspace.shipping_notice import workspace_shipping_notice_bp
|
||||
|
||||
import app.api.workspace.shipping_process_gate # noqa: F401
|
||||
|
||||
__all__ = [
|
||||
'workspace_announcements_bp',
|
||||
'workspace_file_check_bp',
|
||||
|
||||
@@ -1316,6 +1316,81 @@ def workspace_set_shipping_order_review_inventory_candidates(case_no: str, line_
|
||||
return _ok(result)
|
||||
|
||||
|
||||
@workspace_shipping_notice_bp.route(
|
||||
'/shipping-notice/order-review-cases/<case_no>/draft/inventory-routing/reroute',
|
||||
methods=['POST'],
|
||||
)
|
||||
def workspace_reroute_shipping_order_review_inventory_routing(case_no: str):
|
||||
"""
|
||||
按最新已发布规则重算库存源路由;不重新识别 PDF,不静默覆盖人工决定。
|
||||
|
||||
核心逻辑:可选 line_key;携带 expected_draft_version 与幂等键。
|
||||
"""
|
||||
user = _require_user()
|
||||
from app.services.shipping_notice.order_review_draft_service import reroute_inventory_routing
|
||||
|
||||
payload = _review_draft_payload(
|
||||
user_id=user.id, case_no=case_no, action='inventory_routing_rerouted',
|
||||
)
|
||||
result = reroute_inventory_routing(
|
||||
user_id=user.id,
|
||||
case_no=case_no,
|
||||
expected_draft_version=payload.get('expected_draft_version'),
|
||||
idempotency_key=payload.get('idempotency_key'),
|
||||
line_key=payload.get('line_key'),
|
||||
)
|
||||
db.session.commit()
|
||||
return _ok(result)
|
||||
|
||||
|
||||
@workspace_shipping_notice_bp.route(
|
||||
'/shipping-notice/order-review-cases/<case_no>/draft/lines/<line_key>/inventory-routing/adopt-suggestion',
|
||||
methods=['POST'],
|
||||
)
|
||||
def workspace_adopt_shipping_order_review_inventory_routing_suggestion(case_no: str, line_key: str):
|
||||
"""采用当前规则建议为有效库存源候选。"""
|
||||
user = _require_user()
|
||||
from app.services.shipping_notice.order_review_draft_service import adopt_inventory_routing_suggestion
|
||||
|
||||
payload = _review_draft_payload(
|
||||
user_id=user.id, case_no=case_no, action='inventory_routing_suggestion_adopted',
|
||||
)
|
||||
result = adopt_inventory_routing_suggestion(
|
||||
user_id=user.id,
|
||||
case_no=case_no,
|
||||
line_key=line_key,
|
||||
expected_draft_version=payload.get('expected_draft_version'),
|
||||
idempotency_key=payload.get('idempotency_key'),
|
||||
reason=payload.get('reason'),
|
||||
)
|
||||
db.session.commit()
|
||||
return _ok(result)
|
||||
|
||||
|
||||
@workspace_shipping_notice_bp.route(
|
||||
'/shipping-notice/order-review-cases/<case_no>/draft/lines/<line_key>/inventory-routing/keep-manual',
|
||||
methods=['POST'],
|
||||
)
|
||||
def workspace_keep_shipping_order_review_inventory_routing_manual(case_no: str, line_key: str):
|
||||
"""明确保留人工库存源决定并拒绝规则建议。"""
|
||||
user = _require_user()
|
||||
from app.services.shipping_notice.order_review_draft_service import keep_manual_inventory_routing
|
||||
|
||||
payload = _review_draft_payload(
|
||||
user_id=user.id, case_no=case_no, action='inventory_routing_manual_kept',
|
||||
)
|
||||
result = keep_manual_inventory_routing(
|
||||
user_id=user.id,
|
||||
case_no=case_no,
|
||||
line_key=line_key,
|
||||
expected_draft_version=payload.get('expected_draft_version'),
|
||||
idempotency_key=payload.get('idempotency_key'),
|
||||
reason=payload.get('reason'),
|
||||
)
|
||||
db.session.commit()
|
||||
return _ok(result)
|
||||
|
||||
|
||||
@workspace_shipping_notice_bp.route('/shipping-notice/order-review-cases/<case_no>/confirm', methods=['POST'])
|
||||
def workspace_confirm_shipping_order_review_case(case_no: str):
|
||||
"""确认一份 PDF 草稿并独立建单;提交后复用 auto_progress 自动推进。"""
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
"""
|
||||
backend/app/api/workspace/shipping_process_gate.py
|
||||
|
||||
用途:货代全门禁恢复 Workspace API(Execution 读、Gate 列表、恢复动作、审计事件)。
|
||||
依赖:workspace_shipping_notice_bp、process_gate_recovery_service、process_gate_api_service、
|
||||
process_gate_access_service、AppError 全局 handler。
|
||||
逻辑:薄路由鉴权与 POST 体 forbidden-key 校验;写动作委托 request_gate_action;
|
||||
403 时 access_service 写入 process_gate_permission_denied 审计。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from flask import g, request
|
||||
|
||||
from app import db
|
||||
from app.api.workspace.shipping_notice import _ok, _require_user, workspace_shipping_notice_bp
|
||||
from app.core.errors import ValidationError
|
||||
from app.services.shipping_notice.process_gate_access_service import assert_execution_visible_to_user
|
||||
from app.services.shipping_notice.process_gate_api_service import (
|
||||
get_workspace_process_execution,
|
||||
list_workspace_process_gate_audit_events,
|
||||
list_workspace_process_gates,
|
||||
)
|
||||
from app.services.shipping_notice.process_gate_recovery_service import get_execution_by_no, request_gate_action
|
||||
|
||||
|
||||
def _query_int(name: str, default: int) -> int:
|
||||
"""读取可选整数查询参数。"""
|
||||
raw = request.args.get(name)
|
||||
if raw in (None, ''):
|
||||
return default
|
||||
try:
|
||||
return int(raw)
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ValidationError(f'{name} must be an integer') from exc
|
||||
|
||||
|
||||
def _parse_gate_action_body() -> dict:
|
||||
"""
|
||||
校验 POST 恢复动作请求体。
|
||||
|
||||
核心逻辑:仅允许 action_code / expected_gate_version / idempotency_key / payload;
|
||||
顶层与 payload 均禁止 execution 控制字段。
|
||||
|
||||
Returns:
|
||||
规范化后的 action 参数字典。
|
||||
|
||||
Raises:
|
||||
ValidationError: 缺字段或含 forbidden keys。
|
||||
"""
|
||||
body = request.get_json(silent=True) or {}
|
||||
if body and not isinstance(body, dict):
|
||||
raise ValidationError('Request body must be an object')
|
||||
forbidden = {'start_stage', 'target_status', 'worker_name', 'tool_name', 'next_node', 'sql', 'start_gate'}
|
||||
if forbidden.intersection(set(body.keys())):
|
||||
raise ValidationError(
|
||||
'Client must not submit execution controls',
|
||||
code='SHIPPING.PROCESS_GATE_ACTION_FORBIDDEN',
|
||||
)
|
||||
action_code = str(body.get('action_code') or '').strip()
|
||||
if not action_code:
|
||||
raise ValidationError('action_code is required', code='SHIPPING.PROCESS_GATE_ACTION_INVALID')
|
||||
if body.get('expected_gate_version') is None:
|
||||
raise ValidationError(
|
||||
'expected_gate_version is required',
|
||||
code='SHIPPING.PROCESS_GATE_ACTION_INVALID',
|
||||
)
|
||||
try:
|
||||
expected_gate_version = int(body.get('expected_gate_version'))
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ValidationError(
|
||||
'expected_gate_version must be an integer',
|
||||
code='SHIPPING.PROCESS_GATE_ACTION_INVALID',
|
||||
) from exc
|
||||
idempotency_key = str(body.get('idempotency_key') or '').strip()
|
||||
if not idempotency_key:
|
||||
raise ValidationError('idempotency_key is required', code='SHIPPING.PROCESS_GATE_ACTION_INVALID')
|
||||
payload = body.get('payload')
|
||||
if payload is not None and not isinstance(payload, dict):
|
||||
raise ValidationError('payload must be an object', code='SHIPPING.PROCESS_GATE_ACTION_INVALID')
|
||||
if isinstance(payload, dict) and forbidden.intersection(set(payload.keys())):
|
||||
raise ValidationError(
|
||||
'Client must not submit execution controls',
|
||||
code='SHIPPING.PROCESS_GATE_ACTION_FORBIDDEN',
|
||||
)
|
||||
return {
|
||||
'action_code': action_code,
|
||||
'expected_gate_version': expected_gate_version,
|
||||
'idempotency_key': idempotency_key,
|
||||
'payload': payload if isinstance(payload, dict) else None,
|
||||
}
|
||||
|
||||
|
||||
@workspace_shipping_notice_bp.route(
|
||||
'/shipping-notice/process-executions/<execution_no>',
|
||||
methods=['GET'],
|
||||
)
|
||||
def workspace_get_process_execution(execution_no: str):
|
||||
"""读取当前用户可见的 Process Execution 工作台投影。"""
|
||||
user = _require_user()
|
||||
execution = get_execution_by_no(execution_no)
|
||||
assert_execution_visible_to_user(user, execution)
|
||||
return _ok(get_workspace_process_execution(execution))
|
||||
|
||||
|
||||
@workspace_shipping_notice_bp.route(
|
||||
'/shipping-notice/process-executions/<execution_no>/gates',
|
||||
methods=['GET'],
|
||||
)
|
||||
def workspace_list_process_gates(execution_no: str):
|
||||
"""列出 Execution 下全部 Gate 的用户安全投影。"""
|
||||
user = _require_user()
|
||||
execution = get_execution_by_no(execution_no)
|
||||
assert_execution_visible_to_user(user, execution)
|
||||
return _ok(list_workspace_process_gates(execution))
|
||||
|
||||
|
||||
@workspace_shipping_notice_bp.route(
|
||||
'/shipping-notice/process-executions/<execution_no>/gates/<gate_code>/actions',
|
||||
methods=['POST'],
|
||||
)
|
||||
def workspace_post_process_gate_action(execution_no: str, gate_code: str):
|
||||
"""
|
||||
请求执行服务端下发的门禁恢复动作。
|
||||
|
||||
核心逻辑:校验权限与 POST 契约后调用 request_gate_action;同事务提交。
|
||||
"""
|
||||
user = _require_user()
|
||||
execution = get_execution_by_no(execution_no)
|
||||
assert_execution_visible_to_user(user, execution, write=True, gate_code=gate_code)
|
||||
parsed = _parse_gate_action_body()
|
||||
result = request_gate_action(
|
||||
execution_no=execution.execution_no,
|
||||
gate_code=gate_code,
|
||||
action_code=parsed['action_code'],
|
||||
expected_gate_version=parsed['expected_gate_version'],
|
||||
idempotency_key=parsed['idempotency_key'],
|
||||
actor_type='admin' if str(user.role or '').lower() == 'admin' else 'user',
|
||||
actor_id=int(user.id),
|
||||
payload=parsed['payload'],
|
||||
)
|
||||
db.session.commit()
|
||||
return _ok(result)
|
||||
|
||||
|
||||
@workspace_shipping_notice_bp.route(
|
||||
'/shipping-notice/process-executions/<execution_no>/audit-events',
|
||||
methods=['GET'],
|
||||
)
|
||||
def workspace_list_process_gate_audit_events(execution_no: str):
|
||||
"""分页返回 Execution 关联的 PROCESS_GATE 审计事件(用户可见字段)。"""
|
||||
user = _require_user()
|
||||
execution = get_execution_by_no(execution_no)
|
||||
assert_execution_visible_to_user(user, execution)
|
||||
payload = list_workspace_process_gate_audit_events(
|
||||
execution,
|
||||
page=_query_int('page', 1),
|
||||
page_size=_query_int('page_size', 50),
|
||||
)
|
||||
return _ok(payload)
|
||||
Reference in new issue
Block a user