# 货代发货通知 — 错误可解释性契约(阶段 0) > **权威契约文档**:冻结全链路分层原因码、统一投影结构、责任方与下一步动作。 > **代码真相源**:`backend/app/services/shipping_notice/error_explainability_contract.py`; > Gate 阻断归属仍以 [`process_gate_contract.py`](../../../backend/app/services/shipping_notice/process_gate_contract.py) / [process-gate-recovery-contract.md](./process-gate-recovery-contract.md) 为准。 > **原则**:原因码由后端事实产生;前端不得按底层执行字段猜原因;未知原因必须返回 `unknown_business_failure` 并附 `request_id`。 契约版本:`shipping-error-explainability-v1` --- ## 1. 阶段边界 | 阶段 | 内容 | 状态 | | --- | --- | --- | | **阶段 0(本文)** | 原因码分层冻结、统一投影结构、类型/i18n/文档/测试对齐;库存源三类语义固化 | **已完成** | | 阶段 1 | 附件上传、解析、订单表识别原因接线 | **已完成**(附件 explainability、DOCUMENT_PARSE 全量 EMITTED 含别名码) | | 阶段 2 | 复核、客户、Reservation、建单与库存路由展示闭环 | **已完成**(Gate findings 同步;ORDER_FIELD/BUILD 全量 EMITTED;四语言) | | 阶段 3 | 履约、库存 RPA、Excel、货代上传原因细化 | **已完成**(prep/match/excel 全量 EMITTED,惟 `inventory_uom_conversion_failed` 预留) | | 阶段 4 | 全链路审计与审计中心补强 | **已完成**(§9 矩阵 + `AUDIT_COVERAGE_MATRIX` 源码覆盖率 100%;审计中心业务/技术分层;库存源编码始终可见) | > 说明:冻结全集仅余 `inventory_uom_conversion_failed`(尚无 UOM 换算实现,预留)。其余分层集合均为 `EMITTED == frozen`。 > **专项判定**:**达标**。剩余项为已声明预留(UOM)、可用性微调与运行态 E2E 增强,不阻塞验收。 > 契约单测默认 `pure_unit`(不拉 Flask/DB);仅库存源集成断言显式请求 `app`。 --- ## 2. 统一投影结构 所有用户可见失败 / 等待 / 复核原因最终收敛为: ```json { "reason_code": "source_disabled", "message": "库存源 DSV360_AWSSG 未启用", "details": { "source_code": "DSV360_AWSSG", "matched_rule_codes": ["ROUTE_HINT_AWSSG"] }, "responsible_party": "admin", "next_action": "open_inventory_source_review" } ``` | 字段 | 约束 | | --- | --- | | `reason_code` | 稳定、可审计、可测试;别名须经 `EXPLAINABILITY_REASON_ALIASES` 归一 | | `message` | 用户可见文案;前端优先 i18n,后端中文仅为兜底 | | `details` | 业务事实;禁止堆栈、SQL、密钥、内部路径 | | `responsible_party` | `system` / `worker` / `owner` / `admin` / `external` | | `next_action` | 前端按钮与导航唯一依据;须属于已冻结 Gate 动作码 | | `request_id` | `unknown_business_failure` 时必填 | 构造入口:`build_explainability_reason(...)`。 --- ## 3. 三层原因模型(库存源为首批样板) ```text Layer A Gate.blocking_reason_code └─ 例:inventory_source_unavailable(整单/当前门禁阻断) Layer B line.block_reason └─ 例:no_inventory_source_candidates | inventory_source_unavailable | require_manual_review Layer C unavailable_reasons[source_code] └─ 例:source_disabled | source_not_found | parsing_profile_unavailable ``` ### 3.1 禁止混淆的三类语义 | 语义 | 稳定码 | 用户文案要点 | | --- | --- | --- | | 规则未命中 | `no_inventory_source_candidates` | 未匹配到库存源规则 | | 规则命中但源不可用 | `inventory_source_unavailable`(行/门禁)+ Layer C 明细 | 规则命中的库存源当前不可用 / 未启用 / 无 Profile… | | 规则冲突需人工 | `require_manual_review` | 规则冲突或要求人工确认 | **禁止**新增并列同义码 `no_inventory_source_rule_matched`;方案文档中该名称仅为别名,运行时归一到 `no_inventory_source_candidates`。 ### 3.2 单源不可用码(Layer C) | 原因码 | 已接线 | 责任方 | | --- | --- | --- | | `source_not_found` | ✅ | admin | | `source_disabled` | ✅ | admin | | `source_runtime_unavailable` | ✅ | admin | | `parsing_profile_unavailable` | ✅ | admin | | `source_profile_disabled` | ✅ `classify_inventory_source_codes` 已发射 | admin | | `source_excel_template_unavailable` | ✅ `classify_inventory_source_codes` 已发射 | admin | 单源码**不得**写入 `PROCESS_GATE_REASON_OWNING_GATE`;门禁只使用行级阻断码。 --- ## 4. 全量冻结集合(分阶段接线) 完整集合见 `error_explainability_contract.py`: - `ATTACHMENT_REASON_CODES` (+ `_EMITTED`) - `DOCUMENT_PARSE_REASON_CODES` (+ `_EMITTED`) - `ORDER_FIELD_REASON_CODES` (+ `_EMITTED`) - `ORDER_BUILD_REASON_CODES` (+ `_EMITTED`) - `INVENTORY_SOURCE_LINE_BLOCK_REASON_CODES` (+ `_EMITTED`) - `INVENTORY_SOURCE_UNAVAILABLE_REASON_CODES` (+ `_EMITTED`) - `INVENTORY_PREP_REASON_CODES` (+ `_EMITTED`) - `INVENTORY_MATCH_REASON_CODES` (+ `_EMITTED`) - `EXCEL_UPLOAD_REASON_CODES` (+ `_EMITTED`) - `TASK_CLOSE_REASON_CODES` (+ `_EMITTED`) `constants.py` 再导出上述集合,供 Tool / Workspace 引用。 ### 4.1 与既有 Gate 码的别名 | 方案/文档名 | 运行时稳定码 | | --- | --- | | `no_inventory_source_rule_matched` | `no_inventory_source_candidates` | | `order_freeze_not_ready` | `freeze_not_ready` | | `document_layout_parse_failed` | `layout_parse_failed` | | `document_profile_not_matched` | `profile_not_matched` | | `document_profile_ambiguous` | `profile_ambiguous` | | `document_profile_drifted` | `profile_drift` | | `manual_rejected` | `task_rejected_by_user` | | `effective_reservation_missing` | `reservation_missing` | | `reservation_duplicated_in_order` | `reservation_duplicate` | --- ## 5. i18n 与前端类型 | 表面 | 位置 | | --- | --- | | 订单复核行级阻断 | `locales/*/chat.json` → `shippingOrderReview.buildEligibility.blockReasons` | | 单源不可用 | `locales/*/chat.json` → `shippingOrderReview.inventoryRouting.unavailableReasons` | | 工作台门禁 | `locales/*/workspace.json` → `shippingNotice.processGate.reasons` | | 审计中心原因 | `locales/*/admin.json` → `shippingAudit.labels.reasonCodes` | | TS 类型 | `ShippingExplainabilityReason`、`ShippingInventorySource*ReasonCode`(`shipping.api.types.ts`) | 四语言 key 数量必须一致;未知码不得回落展示 Python/SQL/堆栈。 --- ## 6. 验收标准(阶段 0 + 接线) - [x] 分层原因码集合冻结于 `error_explainability_contract.py` - [x] 库存源三类语义文案在后端投影与四语言 workspace i18n 可区分 - [x] 前端类型包含 `reason_code / details / responsible_party / next_action` - [x] 单源不可用码不进入 Gate owning 映射(契约自检) - [x] `unknown_business_failure` 强制附 `request_id` 语义 - [x] Gate 用户投影:`technical_retry_exhausted` 对齐契约且 `next_action=open_order_review` - [x] 未知失败仅附真实 `request_id`;`execution_no` 写入 `details`,禁止冒充 request_id - [x] 抽取失败/取消分别映射 `order_extraction_failed` / `order_extraction_cancelled` - [x] 门禁卡片展示 `explainability.details` + 独立下一步说明;库存源复核展示规则码与源编码 - [x] 已接线库存源原因码具备自动化测试场景(见测试清单) - [x] 附件不完整 ConflictError 附带稳定 `reason_code` + `explainability` - [x] 复核校验同步客户/Reservation/重复订单/库存路由 Gate findings - [x] 处置/履约/门禁失效审计顶层 `reason_code`(§9) - [x] 四语言 blockReasons / processGate.reasons / admin reasonCodes 对齐 EMITTED 主集 - [x] 冻结全集除预留 UOM 换算码外均已 EMITTED;分类器与关键路径具备自动化测试 - [x] 审计中心桌面表无强制横向滚动;Case/Task 业务关联列与 WF/Item/Run 行内次级芯片常显(无需额外展开) - [x] 库存源复核界面始终等宽展示 `source_code`;原始/不可用/建议候选用两列标签卡降低密度 - [x] `AUDIT_COVERAGE_MATRIX` 统一断言关键失败/关闭节点顶层 `reason_code` 源码接线 100% - [x] `late_callback_ignored` / 匹配 disposition 低频码具备映射单测;`request_id` 边界(HTTP/Worker/回调)锁定 --- ## 7. 测试清单 | 测试 | 断言意图 | | --- | --- | | `test_error_explainability_contract_integrity` | 契约自检通过(含审计矩阵) | | `test_audit_coverage_matrix_wires_100_percent_in_source` | §9 关键节点源码接线覆盖率 100% | | `test_late_callback_ignored_maps_to_stable_audit_reason` | 迟到回调 → `late_callback_ignored` | | `test_request_id_resolution_keeps_http_worker_callback_boundaries` | request_id 禁止 execution_no 冒充 | | `test_inventory_unavailable_reason_source_not_found` | Layer C = `source_not_found` | | `test_inventory_all_sources_disabled_blocks_as_unavailable` | 全部命中源不可用 → `inventory_source_unavailable`,≠ 规则未命中 | | `test_canonicalize_reason_alias` | 方案别名归一到稳定码 | | `test_order_field_and_reservation_aliases_own_review_gates` | Reservation 别名归一 + owning_gate | | `test_attachment_incomplete_error_details_has_explainability` | 附件不完整 details 含 explainability | | `test_map_attachment_warning_to_reason_code` | 完整性前缀映射到 ATTACHMENT 码 | | `test_emitted_subsets_cover_match_build_and_close` | MATCH/BUILD/CLOSE EMITTED | | `test_plain_created_task_can_be_deleted_and_restored`(disposition) | 删除双写 `task_cancelled_by_user` | | `test_internal_business_task_is_voided_before_recycle_bin` | 作废双写 `task_voided_for_correction` | --- ## 8. 风险控制(摘要) 最大风险:原因码与真实业务事实不一致(例如把「命中但未启用」标成「规则未命中」)。 控制措施: 1. 原因码仅由后端事实投影产生; 2. 前端禁止根据状态猜原因; 3. 每个已接线码绑定测试; 4. 同一用户可见根因只允许一个 owning gate; 5. 文案、动作、审计共用同一投影。 --- ## 9. 审计覆盖举证矩阵(阶段 4) | 业务节点 | 审计入口 | 顶层 `reason_code` | 自动化证据 | | --- | --- | --- | --- | | 任务删除(纯误建) | `shipping_task_disposition` | `task_cancelled_by_user` | disposition 测试 + `AUDIT_COVERAGE_MATRIX` | | 任务作废并删除 | 同上 | `task_voided_for_correction` | disposition 测试 + 矩阵 | | 任务归档 | 同上 | `task_archived` | disposition 测试 + 矩阵 | | Candidate 驳回 | `candidate_rejected` | `task_rejected_by_user` | 矩阵 + 别名 `manual_rejected` | | Excel 生成成功 | `excel_generated` | `excel_downloaded` | 矩阵源码标记 | | 人工确认上传 | `upload_manual_confirmed` | `task_closed_after_upload` | 矩阵源码标记 | | RPA 上传成功 | `rpa_upload_result` | `task_closed_after_upload` | 矩阵源码标记 | | Gate 阻断/通过 | `process_gate_blocked/passed` | `blocking_reason_code` | 矩阵 `reason_code=gate_reason` | | 下游门禁失效 | `process_gate_downstream_invalidated` | `gate_invalidated` | 矩阵源码标记 | | 迟到 RPA 回调 | `rpa_callback_rejected` | `late_callback_ignored` | 矩阵 + tool_common 映射单测 | | 库存路由 Gate | findings.details + Gate audit | 行级阻断码 + Layer C | draft sync | | 客户/Reservation/重复 | Gate Attempt | ORDER_FIELD EMITTED | `_sync_review_gates_safe` | | 附件完整性拒绝 | ConflictError.details | ATTACHMENT EMITTED | attachment_integrity 测试 | | 纯生命周期(execution created / attempt started / draft changed) | 对应事件 | 允许为空 | 矩阵 `allow_empty=True` | 完整宣称:`AUDIT_COVERAGE_MATRIX` + `test_audit_coverage_matrix_wires_100_percent_in_source` 证明上表失败/关闭类节点均具备顶层 `reason_code` 或明确「允许为空」政策;审计中心可按码筛选。