Files
syops/docs/features/shipping-notice/display-and-button-contract.md
T

367 lines
34 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 货代发货通知 — 工作台「展示 & 按钮」唯一契约(冻结 2026-09)
> **本文件是订单处理工作台在「展示数据」与「任务级/明细级按钮」两方面唯一权威契约。**
> 结论基于现有实现(`shipping_processing_projection` / `ui_state` / `process_gate` /
> `stage_context.preparation`),**不新增投影/抽象**,只做「单一来源 + 去冗余」。
> 后端只读投影与前端所有消费方都必须遵守,禁止再私自分叉。
---
## 0. 一句话结论
- **展示单源**:右「当前任务」卡、5 阶段页签、批次中心卡 **全部** 以 `shippingProcessingProjection`
(含其 `ui_state` 子 DTO)为单一来源;`formalTaskDetail.ui_state` **不再**参与「当前阶段 / 用户状态 /
阻断 / 动作」展示,只保留它独有的**工作面只读事实**(`stage_context.preparation`、`edit_window`、
`steps/todo/checkpoint`、`business_no`、`export_artifacts`)。
- **按钮分域**:只有 **1 个**动作是**明细级**(`override_line_inventory_source`,已取代旧 D5
`select_line_inventory_source`),只出现在库存表格逐行;其余全部**任务级**,只出现在右「当前任务」卡。
- **三库存动作不重复**:`retry_inventory_preparation`(重跑 RPA)、`retry_inventory_matching`
(重匹配已抓取库存,不跑 RPA)、`reload_inventory_result`(只重读已落盘结果,不跑 RPA/不重算)。
- ~~**真正要删的冗余**:`select_line_inventory_source`(旧 D5 多文件冲突逐行选源,被
`override_line_inventory_source` 取代)。~~ **已完成删除**(后端动作码/处理器/校验 + 前端类型成员均移除)。
- **明细级按钮的数据源**:`ui_state.stage_context.preparation.lines[].line_allowed_actions /
inventory_allowed_actions`(后端**必须**下发;当前缺失导致明细级按钮不渲染,见 §5)。
---
## 1. 现有投影「角色」归类
| 编号 | 形状 | 产出方 | 唯一用途 | 稳态/运行态 | 底层唯一事实源 |
|---|---|---|---|---|---|
| A | `shipping_processing_projection.order_task`(含 `ui_state`) | `_project_order_task_work_item` → `reconcile_processing_projection_facts` → `apply_user_projection_fields` → `_with_processing_ui_state` | 右「当前任务」卡 + 批次中心卡 + 会话卡 | 稳态只读 | `ShippingOrderTask.stage/status/pending_human_reason/review_status/progress_version/active_process_execution_id` + `ShippingProcessExecution.current_gate_code/status` + `ShippingProcessGate` + `ShippingNotice.lifecycle_status` + 库存准备/导出/上传 Attempt |
| B | `formalTaskDetail.ui_state` | `build_task_ui_state` → `resolve_task_ui_state` → `_task_lifecycle` → `_unified_actions` | 工作面只读事实:`stage_context.preparation`(表格/编辑窗)、`edit_window`、`steps/todo/checkpoint`、`business_no` | 稳态只读 | `ShippingNotice.lifecycle_status/task_no/review_status` + `ShippingOrderTask` + 最新 Export/Upload/Verification/Match/Preparation 行 |
| C | `process_gate` 摘要 | `project_user_gate_summary` → `_resolve_recovery_next_action` → `recovery_targets` | 唯一「当前能做什么 + 为何阻断」裁决(`execution_no/evaluation_version/recovery_targets` 供**写**请求) | 稳态只读 | `ShippingProcessExecution` + `ShippingProcessGate` + `PROCESS_GATE_DEFINITIONS.default_next_actions` |
| D | `stage_context.preparation` | `_build_stage_context` → `preparation` | 通知准备面板的库存事实(`frozen_order_lines`/`lines[]`/`source_options`/`inventory_sources`/`capabilities`) | 稳态只读 | `ShippingNoticeLineInventoryMatch(is_current)` + `ShippingInventoryVerification` + `ShippingInventoryPreparationAttempt` + `ShippingExportArtifact` + `ShippingUploadAttempt` |
| E | `lifecycle_stage`/`stage_label_key` | `project_lifecycle`(`lifecycle_stage/entries/authority`)+ `project_stage_for_task`(`current_stage.code/label_key`) | 5 阶段页签/入口 + 技术阶段码+文案键 | 稳态只读 | `ShippingOrderTask.stage`(优先)+ `map_run_to_processing_stage` + `ShippingProcessExecution.current_gate_code` + 核验上传成功 |
**关键澄清(根因)**:`stage` 这个字段名在 A 的 `ui_state` 是**技术阶段码**
(`shipping_processing_projection.py:3128`),在 B 的 `build_task_ui_state` 是 **5 阶段 DTO**
(`ui_state.py:1117`)。**同一键、两个无关值域**。契约用不同且唯一的键区分:
`stage/stage_label_key/current_stage.code` 只承载**技术阶段码**;`lifecycle_stage/lifecycle_entries/lifecycle_authority`
只承载 **5 阶段业务标签**。
---
## 2. 展示契约:每个字段的唯一来源
> 规则:右卡与 5 阶段页签的字段**全部读 A**,不再读 B。B 只保留工作面事实。
> 稳态/运行态只影响「是否可能为空/待刷新」,**不改变字段来源**。
| 展示值 | 唯一来源(单源) | 稳态/运行态 | 落点 UI |
|---|---|---|---|
| 5 阶段当前阶段 | `shippingProcessingProjection.ui_state.lifecycle_stage` | 稳态 | 5 阶段页签头 + 右卡 |
| 5 阶段入口/授权 | `...ui_state.lifecycle_entries` / `.lifecycle_authority` | 稳态 | 5 阶段页签 |
| 技术阶段码+文案键 | `shippingProcessingProjection.stage` / `.stage_label_key` | 稳态 | 右卡「当前门禁」行、批次卡 |
| 用户状态 | `...ui_state.status` | 稳态 | 右卡「状态」行 / 处理详情抽屉 |
| 阻断原因(下一步) | `...ui_state.blocking_reason` | 稳态 | 右卡「下一步」 |
| Gate 状态 | `shippingProcessingProjection.current_gate.status` | **运行态** | 右卡(区分未入队/运行/待外部回执) |
| 主动作/恢复动作 | `...ui_state.primary_action` / `.secondary_actions`(+任务级 `stage_context.preparation.capabilities`) | **运行态** | 右卡主按钮 + 恢复按钮 |
| 库存准备时间线 | `shippingProcessingProjection.inventory_preparation` + `.facts` | **运行态** | 通知准备时间线 |
| 工作面表格行/改源/编辑 | `formalTaskDetail.ui_state.stage_context.preparation.*` | 稳态 | 通知准备表格(**明细级**) |
| 编辑窗(D10) | `formalTaskDetail.ui_state.edit_window` | 稳态 | 编辑面 |
| 步骤条/todo/checkpoint | `formalTaskDetail.ui_state.steps/todo/checkpoint` | 稳态 | 顶部步骤条 |
| 业务号/导出产物 | `formalTaskDetail.business_no` / `export_artifacts` | 稳态 | 上传面 |
### 稳态 vs 运行态
- **稳态**(`stage/lifecycle_stage/status/blocking_reason`):由 `ShippingOrderTask.stage/status` +
`ShippingProcessExecution.current_gate_code` + `project_lifecycle` 派生,不随 RPA 运行而变。
- **运行态**(`current_gate.status`、`ui_state.primary_action/secondary_actions`):由
`ShippingProcessExecution/ShippingProcessGate` 当前行派生,随 RPA/执行推进。前端轮询 A
(`startWorkflowProgressPolling` → `refreshShippingProcessing` → `shippingProcessingProjection`)
刷新,`progress_version` 单调递增驱动右卡刷新。
### 2.1 A↔C 等价性验证结果(已用 `tests/test_shipping_projection_gate_equivalence.py` 锁定)
> **结论:A 与 C 当前只有 2 处漂移,均为 A 侧错误;flatten A(§2.2)只须消除这两处,其余 A==C 已成立。**
> 该测试跑真实 A 管线(`_project_order_task_work_item`)并行重调 C(`project_user_gate_summary`),
> 规范场景严格断言相等;2 处已知漂移以 `xfail(strict=True)` 固定,flatten 实现后必须转绿。
| # | Gate | 漂移点 | A(错) | C(对) | 影响 |
|---|---|---|---|---|---|
| D1 | `artifact_generation` | `primary_action.code` | `regenerate_excel`(A STEP7 别名 `:999`) | `regenerate_excel_artifact` | A 渲染出的按钮写侧 `request_gate_action`(`process_gate_recovery_service.py:212`,只认 `PROCESS_GATE_ACTION_CODES`)会拒绝 → 会话卡/批次进度中心出现点了没反应的按钮 |
| D2 | `inventory_preparation` blocked_retryable | `blocking_reason.code` | `technical_failure_present`(A STEP6 坍缩 `:945-950`) | `inventory_preparation_failed` | A 丢失了 C 已裁决的具体原因,右卡/批次卡「下一步」文案退化 |
**flatten A 的边界(重要)**:A 的 `user_status`(`_USER_PROCESSING_STATUSES`,STEP3)是
`completed/cancelled/failed/retryable/waiting_human/running` 六值**展示分组枚举**,C 不产出、
B 也不产出 → 遗留 A 职责,**不得**纳入 flatten。flatten 只动 `primary_action.code`(用 C 的
`process_gate['primary_action']['code']` 或规范码,去别名)与 `blocking_reason.code/i18n_key`
(用 C 的 `blocking_reason_code` + `explainability` 的 i18n)。
**依赖/前置**:C 摘要 `payload['process_gate']` 仅 `_project_order_task_work_item`(`shipping_processing_projection.py:1857-1860`)
路径写进 payload,Intake/min 投影路径(`:983`/`:4695`)无 C。故 flatten 必须**条件化**:
`payload.get('process_gate')` 为 dict 时消费 C,否则保留现有推导(Intake 无 execution 本就无 Gate)。
**flatten 后完整等价性已锁定(A==B==C)**:`tests/test_shipping_projection_gate_equivalence.py` 现 15 用例全绿,
覆盖 `inventory_sufficiency` / `inventory_preparation` / `artifact_generation` / `freight_upload_rpa` 四 Gate ×
A↔C(primary_action/blocking_reason)+ B↔C(actions primary=next_action、primary+secondary 并集=allowed_actions)
+ 生命周期阶段 A↔B + §1.5 明细按钮字段 + N+1 守门。
B 的 `build_task_ui_state` 既直接消费 `project_user_gate_summary`(`_process_gate_user_summary_for_task`),故 B==C 结构性成立;
A 经 STEP8 flatten 后亦 == C。三点同源,右卡/对话流/批次进度中心不再各自为政。
**动作码 A/B/C 表示差异(已登记,阻塞 §1.1 与 manualUpload 收敛)**:C(`project_user_gate_summary.allowed_actions`)
是唯一授权来源,`mark_manual_uploaded` 在 **C** 与 **B**(`_unified_actions`)中就用 `mark_manual_uploaded` 本码,
但 **A**(`_processing_ui_state_from_item.secondary_actions`)经 `derive_available_actions`
(`processing_projection_invariants.py:476`)把 `mark_manual_uploaded` 归一为 **`open_manual_upload`** 别名。
等价性 harness `test_a_secondary_actions_carry_mark_manual_uploaded`(17/17 全绿)验明:`freight_upload_rpa` 门禁下
`C.allowed_actions` 含 `mark_manual_uploaded`、`A.secondary_actions=[]`(用 `open_manual_upload` 别名)。
**含义**:前端 `manualUploadRegistrationAuthorized` 若改为读 A 会匹配不到 `mark_manual_uploaded` → 授权失效。
**根修法**(高 blast radius,需单独评估):让 A 的 `secondary_actions` 透传 C 的原始 Gate 码(不再经
`derive_available_actions` 归一为 `open_manual_upload`),使 A==B==C 的动作码逐字一致。此改动涉及
`derive_available_actions`/手动上传流,须谨慎并单独回归。
**生命周期阶段 A↔B 同源确认(无伪漂移)**:A(`_processing_ui_state_from_item`)与 B(`_task_lifecycle`)
**都**调用 `project_lifecycle`。`LIFECYCLE_STAGE_BY_GATE`(`lifecycle_projection.py:30`)是正式 Gate→五阶段
业务码的唯一表,已验证:`freight_upload_rpa`→`freight_upload`、`manual_upload_handoff`→`freight_upload`
(用户确认的契约)。等价性 harness `test_gate_maps_to_lifecycle_stage_b`(5 Gate)在 **B 真实路径
(build_task_ui_state on Notice,processing_stage=None → Gate 兜底)** 下全部命中正确业务码。前一轮曾误判
`freight_upload_rpa` 为 B 滞后,实为 harness 用 `ShippingOrderTask`(含 stale stage)走 B、而 A 经 reconcile
推进 stage 的差异——**非真实漂移**。因此后端 gate→lifecycle 映射无需改动,前端 5 阶段页签/右卡以 A 为单源
属**纯收敛**(行为不变)。
### 2.5 A↔B/C 动作码与生命周期形状归一化(后端,§1.1 与 manualUpload 前置)
> **一句话**:A 的 `ui_state` 在「动作码」与「生命周期周边字段」上与 B/C **逐字一致**后,
> 前端把 §1.1(`lifecycleUiState` 读 A)与 manualUpload(读 A)切过去才零差异、测试全绿。
> **本节点是「先补契约、后动代码」的实施前置**,已用等价性 harness 实测定位两处不一致:
> ① 动作码 `mark_manual_uploaded` 被 A 别名成 `open_manual_upload`;
> ② A 的阶段入口 `entries` 与 A 的阻断说明 `blocker` 形状与 B 不同。
#### 2.5.1 现状(三源逐一对比)
| 字段 | A(`shippingProcessingProjection.ui_state`) | B(`formalTaskDetail.ui_state`) | C(`project_user_gate_summary`) | 差异 |
|---|---|---|---|---|
| 动作码 `mark_manual_uploaded` | `secondary_actions` 里是 **`open_manual_upload`**(别名) | `actions.secondary` 里是 **`mark_manual_uploaded`** | `allowed_actions` 里是 **`mark_manual_uploaded`** | ⚠️ A≠B/C |
| 阶段入口 | `lifecycleEntries`(`[]如{code,state,targetSurface}`) | `lifecycle_entries`(同形状) | `lifecycle_entries`(同形状) | ✅ 已同形 |
| 阻断说明 `blocker` | `blocking_reason`(`{code,i18nKey,details}`) | `reason.message`(已翻译文案) | `blocking_reason_code`+`explainability` | ⚠️ A≠B |
| 阶段码 | `lifecycleStage` | `stage.code` / `lifecycle_stage` | 由 `project_lifecycle` 统一 | ✅ 同源 |
**已确认同源的部分**:`lifecycle_stage`(阶段码)3 源均用 `project_lifecycle`,等价性 harness
`test_gate_maps_to_lifecycle_stage_b`(5 Gate)已证明一致。**真正需要归一的是动作码(第 1 行)与 blocker 文案(第 3 行)。**
#### 2.5.2 差异①:`mark_manual_uploaded` 被 A 别名成 `open_manual_upload`(**已归一,验证通过**)
- **根因**:`processing_projection_invariants._ACTION_CODE_ALIASES['mark_manual_uploaded'] = 'open_manual_upload'`(`:88`)
与 `derive_available_actions`(`:476-477`)把 `gate_action ∈ {open_manual_upload,wait_manual_upload,mark_manual_uploaded}`
`_push('open_manual_upload')`,导致 A 的 `secondary_actions` 输出别名,而非原始 Gate 码。
- **影响**:`manualUploadRegistrationAuthorized`(前端读 A 找 `mark_manual_uploaded`)查不到 → 手动上传按钮失效。
右卡 `currentTaskRecoveryActions`(§2.4 已改读 A)同样会漏掉该恢复入口。
- **修法(已实施 + 验证)**:`shipping_processing_projection` 新增 `_process_gate_allowed_action_codes(item)`,
`_processing_ui_state_from_item` 用它从 C 摘要(`payload['process_gate'].allowed_actions` / `action_presentation[].code`)
取**原始 Gate 码**构造 `secondary_actions`(不再经 `_ACTION_CODE_ALIASES` 归一为 `open_manual_upload`)。
无 C 时回退 `available_actions`(Intake/min 投影路径)。
- **验收(已绿)**:`test_a_secondary_actions_carry_mark_manual_uploaded`(等价性 harness)在 `freight_upload_rpa`
门禁下断言 **A.secondary_actions 含 `mark_manual_uploaded`** 且与 `C.allowed_actions` 同集;归一前 RED、归一后 GREEN。
后端 144 投影 + 前端 376 全绿无回归。
#### 2.5.3 差异②:A 的 `blocker`(阻断说明)形状与 B 不同
- **现状**:A 的 `blocking_reason` 是 `{code,i18nKey,details}`;B 的 `reason.message` 是「已翻译文案」。
前端 `lifecycleStageItems` 需要的是「可展示的阻断文案字符串」。
- **影响**:§1.1 把 `lifecycleUiState.blocker` 改读 A 时,若直接吐 `i18nKey`,页签下方小字会显示机器码;
若用 `resolveShippingProcessingReasonLabel` 翻译,则需保证 A 的 `blocking_reason.i18nKey/code` 能映射到文案键。
- **修法**:A 的 `ui_state` 增加/规范 `blocker` 字段(或让前端统一用 `resolveShippingProcessingReasonLabel`
+ `te/t` 从 `blocking_reason` 解析),使前端读 A 时无需区分 A/B 形状。
- **⚠️ §1.1 前置的真实阻塞(实测,Round 18/19)**:§2.5.3 的「形状」并非唯一阻塞。用方案甲(A 绝对优先)切换
`lifecycleUiState` 读 A 后,`ShippingOrderReviewView.spec.ts` 实测 **20 个用例失败**(非 14),横跨上传/货代阶段
gating,如 `renders one Notice card... freight upload`、`aggregates freight upload by business_no`、
`sends selected business_nos`、`keeps upload-complete evidence`、`allows inspecting a completed order-review stage`。
**根因**:这些测试刻意把 B/`reviewQueue` 设为 `upload_complete`/`freight_upload`、把 A 设为别处,并断言
**B/reviewQueue wins**;当前吞吐上传工作表、批量 Excel、完成证据的阶段判定**深度依赖 B/reviewQueue 作为权威**。
→ §1.1 不是「14 测试小迁移」,而是「**20 测试 + 整个 upload/freight 阶段 gating 的行为改写**」,回归风险远大于收益。
**建议**:§1.1 按高风险跨源重构**专项处理**(方案甲′:阶段码 A 权威、上传/货代门控保留现有来源,先缩小爆炸面;
或方案丙:登记风险、暂缓,契约其余项已全部落地)。当前代码保持 B/reviewQueue 权威(103/103 绿)。
#### 2.5.4 落地顺序(先守卫后放开,保持套件绿)
1. **加守卫测试(零行为变更)**:在 `test_shipping_projection_gate_equivalence.py` 追加
`A.secondary_actions` 必须透传 `mark_manual_uploaded`(与 `C.allowed_actions` 逐字一致)的断言。
2. **后端归一**:改 `_processing_ui_state_from_item`(透传原始 Gate 码)+ 若需要再补 A 的 `blocker` 字段。
3. **前端切换**:§1.1 `lifecycleUiState` 读 A + `manualUploadRegistrationAuthorized` 读 A。
4. **全量回归**:后端(71 当前)+前端(376 当前)+等价性 harness;迁移仍依赖 B 形状的少量 spec。
5. **性能护栏**:归一只改内存 dict 组装,不新增查询(`project_user_gate_summary` 仍单 work-item 1 次,
`stage_context.preparation` 仍只进详情)。
#### 2.5.5 边界(不改的部分)
- `user_status`(A 独有的六值展示分组枚举)留在 A,**不**纳入归一。
- `open_manual_upload` 作为「聊天/通知级手工上传入口」在 `derive_available_actions` 仍保留(它本就是另一层
展示别名),只是**不得再覆盖 `mark_manual_uploaded` 的原始码**。
- 写契约(`request_gate_action`)仍只认 `mark_manual_uploaded` 等 `PROCESS_GATE_ACTION_CODES`;
前端从 A 读到的动作码必须能与写契约对齐(归一的直接目的)。
### 两个展示 bug 的契约修法
- **「当前门禁 —」**:`resolveShippingProcessingStageLabel('')` 返回 `'—'`(`shippingProcessingLabels.ts:150`),
当 `currentProcessingStageDisplay` 因投影 `stage/ui_state.lifecycle_stage` 均空(且 `!loading`)返回空时触发。
**修法**:已知正式 OT 必有阶段,投影恒有 `stage/lifecycle_stage`;投影未就绪由右卡 `:loading`
(`formalTaskProjectionPending`/`formalTaskDetailLoading`/`caseSwitchLoading`)驱动骨架占位,**禁止**落 `'—'`。
- **「状态待确认」**:`resolveShippingProcessingStatusLabel` 不认识码时的兜底(`labels.ts:69`),
仅被 `ReviewProcessDetailDrawer` 消费(读 `projection.status`)。右卡空态用的是 `statusIdle`(待机)、
表格行数用 `'待确认'`——**三套词汇**。**修法**:状态统一从 `shippingProcessingProjection.ui_state.status`
(受控 6 值)取,禁止给投影塞不受控码;`resolveShippingProcessingStatusLabel` 仅作不受控码兜底。
---
## 3. 按钮契约:任务级 vs 明细级
### 3.1 动作面(~36 个)与作用域
`PROCESS_GATE_ACTION_CODES`(`process_gate_contract.py:349`)∪ `STAGE_CAPABILITY_DEFINITIONS`
(`process_gate_contract.py:1313`)。**只有 2 个是明细级**:
| 动作码 | 作用域 | 宿主 | 位置 |
|---|---|---|---|
| `override_line_inventory_source` | **明细级** | `_handle_override_line_inventory_source`(`process_gate_executor.py:885`) | 库存表格逐行(override 下拉) |
| `select_line_inventory_source` | **明细级(已死,待删)** | 旧 D5 多文件冲突逐行选源 | 仅剩 `LINE_SCOPE_INVENTORY_ACTIONS` 名字,待删 |
| 其余全部 | **任务级** | 右「当前任务」卡 | 主按钮 + 恢复按钮 |
前端唯一判据:`LINE_SCOPE_INVENTORY_ACTIONS = {override_line_inventory_source, select_line_inventory_source}`
(`ShippingOrderReviewView.vue:753`)。凡在此集合内的动作一律不进任务级按钮(右卡主按钮 `:762`、
恢复按钮 `:862` 均排除)。
### 3.2 步骤/状态 → 动作(按 Gate 阻断归属)
主动作唯一裁决:`_resolve_recovery_next_action`(`process_gate_projection.py:130`);侧路:
`gate_side_recovery_actions`(`process_gate_contract.py:494`)。二者与校验层 `request_gate_action`
的 `allowed_side` **共用同一份裁决**(保证「投影放行==校验放行」)。
| Gate 阻断 | 主动作 | 侧路 |
|---|---|---|
| `inventory_preparation` blocked_manual/retryable | `retry_inventory_preparation` | — |
| `inventory_preparation` waiting_external | `poll_inventory_preparation` | — |
| `inventory_preparation` RPA 超时/耗尽 | `retry_inventory_matching`(复用快照重匹配,无则 RPA) | `retry_inventory_preparation` |
| `inventory_sufficiency` 正式 OT+已确认库存 | `retry_inventory_matching` | — |
| `inventory_sufficiency` 正式 OT+无确认库存(短缺) | `open_shortage_review` | — |
| `inventory_matching`/`inventory_sufficiency` 建单前 | `open_inventory_match_review`/`open_shortage_review` | `select_line_inventory_source`(多文件冲突) |
| `freight_upload_rpa` 失败 | `retry_freight_upload_rpa` | — |
| `manual_upload_handoff` | `mark_manual_uploaded` | `retry_freight_upload_rpa` |
**任务级常驻能力**(通知准备,`_stage_capabilities_for_task` `ui_state.py:603`):
`retry_inventory_preparation`(有候选/失败源)、`reload_inventory_result`(有已解析源)、
`retry_inventory_matching`(有匹配结果)、`override_line_inventory_source`(有候选源,明细级)。
### 3.3 三库存动作去重结论(已按用户拍板合并为 2 个按钮)
| 动作 | 做什么 | 跑 RPA | 失效范围 | 触发状态 |
|---|---|---|---|---|
| `retry_inventory_preparation` | 直接触发库存 RPA/抓取 | ✅ | 重抓后全链 | 库存准备失败 / 显式重抓 |
| `retry_inventory_matching`(**合并动作**) | 优先复用已有快照并重匹配;**无快照则回退 RPA** | 视情况(无快照时) | 匹配+充足性+Excel+产物 | 库存匹配/充足性 + 库存准备 RPA 超时/耗尽 |
**`reload_inventory_result` 已下线(不再独立成按钮)**:其「复用已有快照」行为并入 `retry_inventory_matching`
(`match_inventory` 内部在无库存行时经 `ensure_inventory` 自动回退 RPA,见 `fulfillment_workflow.match_inventory:1611-1616`)。
原 `inventory_preparation` 主恢复从 `reload_inventory_result` 改为 `retry_inventory_matching`(`process_gate_contract.inventory_preparation_primary_action`),
侧路保留 `retry_inventory_preparation`(强制重抓)。**动作码 `reload_inventory_result` 在写契约仍有效**(防历史/内部调用运行时 400),
但从用户按钮面下线展示。已同步 `gate_side_recovery_actions`/`action_belongs_to_gate`/前端 action presentation 与相关测试。
### 3.4 明细级按钮的数据源(§0 一句话结论的展开)
通知准备表格**每行**的明细级按钮(库存放行、关闭不可履约、行级重抓/重匹配、下载库存)由
`ui_state.stage_context.preparation.lines[].{line_allowed_actions, inventory_allowed_actions, inventory_file_no,
release_required_fields, release_field_resolution_error, decision}` 驱动,**后端必须下发**到该数组。
前端对应门禁:`formalPreparationLineAllows(line, action)`(`ShippingOrderReviewView.vue:2571`):
- `close_unfulfillable` / `release_inventory_shortage` → `line_allowed_actions.includes(action)` 且
`release_inventory_shortage` 需 `!release_field_resolution_error`;
- `retry_inventory_preparation` / `retry_inventory_matching` → `inventory_allowed_actions.includes(action)`;
- 下载库存 → `inventory_file_no` 非空(`downloadFormalPreparationLineInventoryFile` `:2647`)。
> ~~**当前 BUG**:后端 `_build_stage_context` 生成 `preparation.lines[]`(`ui_state.py:837-862`)**未输出**
> 上述 `*_allowed_actions`/`inventory_file_no`/`release_*`/`decision` 字段(它们只存在于 Case 快照
> `line_fulfillment.lines[]`,见 `order_review_fulfillment_decision_service.py:956-973`、`line_inventory_match_projection.py:667-694`),
> 导致 `formalPreparationLineAllows` 恒 false、明细级按钮全部不渲染。~~
> **已修复(§1.5)**:`_build_stage_context` 现已在行循环内用内存 `match_row` 事实调用纯函数
> `project_line_inventory_view` 下发 `line_allowed_actions`/`inventory_allowed_actions`/`inventory_file_no`/
> `user_line_status`/`is_shortage` 等(零逐行 SQL,见 §6 落地清单)。`release_required_fields`/
> `release_field_resolution_error`/`decision` 当前为 fail-closed 默认(`[]`/`null`/`null`)。
---
## 4. 前后端如何配合(冻结)
1. **后端只读投影**:A(`shippingProcessingProjection`+`ui_state`)是唯一**展示+动作**合同;
C(`process_gate`)是唯一**写**裁决(`execution_no/evaluation_version/recovery_targets`);
B/D 只提供**工作面**事实。三者同一派生事实源,后端负责让它们同源不漂移。
2. **前端轮询**:`startWorkflowProgressPolling`(`PROGRESS_POLL_MS`)→ `refreshShippingProcessing`
→ `shippingProcessingProjection`(按 `progress_version` 单调合并)→ 刷新右卡 + 5 阶段页签;
必要时再 `loadFormalTaskDetail` 拉工作面事实。
3. **前端职责**:不用 `formalTaskDetail.ui_state` 推展示/动作;右卡与页签只用 A 的 `uiState`,
工作面表格/编辑窗只用 B 的 `stage_context.preparation`。不自行推导阶段/按钮;`stage` 码不与
`lifecycle_stage` 混淆。
---
## 5. 效能约束(必须遵守,与功能契约同权)
> 核心:展示与按钮单源收敛**不得**以新增查询 / 渲染热路径为代价。`stage_context.preparation`
> 仍**只在任务详情**下发,不进轮询 / 轻量投影。
1. **明细级按钮零 N+1(§3.4)**:`_build_stage_context`(`ui_state.py:735`)已把该任务
`ShippingNoticeLineInventoryMatch(is_current=True)` 全量加载进内存并建好 `match_by_snapshot_line_id`。
`preparation.lines[]` 逐行的 `line_allowed_actions / inventory_allowed_actions / inventory_file_no /
release_required_fields / release_field_resolution_error / decision` 必须**在同一个循环内**、
复用上述**已加载内存事实**结算(调用 `line_inventory_match_projection.resolve_*_allowed_actions`
等纯函数),**禁止逐行 SQL / 逐行调用重序列化器**。复杂度保持 O(行数)。
2. **`stage_context.preparation` 只进任务详情**:不得下放到 `shippingProcessingProjection`(work-item /
轮询 / 轻量投影),避免 N+1 与轮询膨胀。该限制已有(见 `technical.md`),本次收紧为契约级。
3. **flatten C 复用单次 `project_user_gate_summary`**:A(`_order_task_current_gate`)与 B
(`_process_gate_user_summary_for_task`)必须**复用同一份 C 结果**,不得各自再调用 / 再推导
`primary_action / blocking_reason / next_action`,保证单个 work-item 只调用一次、净减重复推导。
4. **poll 热路径不变**:轮询 `PROGRESS_POLL_MS`(当前 `5000`,`useShippingOrderReview.ts:86`)与
`refreshShippingProcessing()` 的调用序列**不因本次收敛改变**;前端读 A 只改读序、不增请求。
(顺带修正过期的「2.5s/3s」注释为实际 `5000`。)
5. **验收护栏**:
- 改前 / 改后对 `GET /shipping-tasks/<no>`(含 `stage_context`)做 SQL 查询次数对比,确认
**不随行数线性增加**(加 `assert_num_queries` 类测试,或手工 `EXPLAIN` 对比)。
- 确认单个 work-item 的 `project_user_gate_summary` 调用次数不变(仍 1 次)。
---
## 6. 落地清单(去冗余,不新增抽象)
- [ ] 前端:`lifecycleUiState`(`ShippingOrderReviewView.vue:267`)优先读 `shippingProcessingProjection.ui_state`,
删除对 `formalTaskDetail.ui_state` 的优先读取(保留 `reviewRun/reviewQueue` 作建单前降级),页签+右卡同源。
**前置 = §2.5 后端归一**:A 与 B 的 `lifecycle_stage` 同码,但 `entries`/`blocker` 形状不同(A 的
`lifecycleEntries`/`blocking_reason` vs B 的 `lifecycle_entries`/`reason.message`)。把 `lifecycleUiState`
改为读 A-first 会因形状差异破坏 14 个 spec。**先按 §2.5 把 A 的 `blocker`/`entries` 归一成 B 口径,再切前端**;
否则只在 stage 码上同源,entry/blocker 仍需 B 的面工件事实。
- [x] 前端:右卡 `currentTaskPrimaryAction`/`currentTaskRecoveryActions`(`:758/:840`)只用
`shippingProcessingProjection.ui_state` 取动作(不再读 `formalTaskDetail.ui_state.actions`);恢复动作 =
`ui_state.secondary_actions` + 任务级 `stage_context.preparation.capabilities`,按 code 去重、剔除明细级。
(已验证:`ShippingOrderReviewView.spec.ts` 103 全绿;注意主动作/恢复动作的 i18nKey 必须统一映射到
`workspace.shippingNotice.processGate.actionCodes.<code>` 卡片域,否则右卡因 `businessActionLabel` 为空 fail-closed。)
- [x] 前端:删除 `formalTaskActions`(`:746`)对右卡的依赖(仅保留 `manualUploadRegistrationAuthorized` 等
真正正式动作使用方)。**`manualUploadRegistrationAuthorized` 已收敛到 A**(§2.5 差异① 归一后,
A 的 `secondary_actions`/`primary_action` 透传原始 `mark_manual_uploaded`);读 A 后与写契约码一致。
已验证 `ShippingOrderReviewView.spec.ts` 103/103 全绿。
- [x] 前端:把「当前门禁」`--` 收敛为「投影未就绪 → 骨架占位」,禁止空阶段落 `'—'`。
(已完成:`resolveShippingProcessingStageLabel` 空 stage 不再返回 `'—'`,改为 `stageLoading` 占位文案;
`ShippingOrderReviewView.spec.ts:4029` 断言同步迁移。已验证 103/103 前端全绿。)
- [x] 前端:`LINE_SCOPE_INVENTORY_ACTIONS`(`:753`)只保留 `override_line_inventory_source`(已删 `select_line_inventory_source`)。
- [x] **后端(明细级按钮,按 §5.1 零 N+1 实现)**:`_build_stage_context`(`ui_state.py:780-862`)让
`preparation.lines[]` 携带 `line_allowed_actions`/`inventory_allowed_actions`/`inventory_file_no`/
`release_required_fields`/`release_field_resolution_error`/`decision`。限定:在现有
`current_match_rows` 循环内**复用内存事实**结算,**禁止**逐行 SQL / 复用 Case `line_fulfillment` 重序列化器。
(已实施 + 验证,见下方 §1.5 条目详情。)
- [x] 后端:删除 `select_line_inventory_source`(已完成)。已被 `override_line_inventory_source`
取代(方案 3·放开:任一启用源 + 行级重匹配,覆盖多文件冲突行)。已删:
`process_gate_contract.py`(`PROCESS_GATE_ACTION_CODES` / `gate_side_recovery_actions` D5 分支 /
`action_belongs_to_gate` 校验)、`process_gate_executor.py`(dispatch + `_handle_select_line_inventory_source`)、
`process_gate_service.py`(并发行级动作集合)、`error_explainability_contract.py`(`inventory_multi_file_conflict`
meta 的 next_action 改为 `override_line_inventory_source`)、`tests/test_shipping_select_line_source.py`(整文件删除)。
前端同批移除:`ShippingProcessingActionIntent` 类型成员 + D5 注释改指 `override_line_inventory_source`。
- [x] 后端:确认 `apply_user_projection_fields` 的 `primary_action/blocking_reason` 与 C 同源(复用 C 裁决,
不再二次推导),消除批次卡/右卡漂移。**已实施 + 验证**(`test_shipping_projection_gate_equivalence.py` 现 10 用例全绿):
- 在 `processing_projection_invariants.py` `apply_user_projection_fields` 末尾新增 **STEP8 条件化消费 C**:
`payload['process_gate']` 存在时 `primary_action.code/blocking_reason.code` 取 C 规范码 + C i18n
(去 D1 `regenerate_excel` 别名、去 D2 `technical_failure_present` 坍缩);无 C 时保留现有推导(Intake/min 投影路径)。
- `user_status`(六值展示分组枚举)是 A 独有,C/B 不产出,**不纳入 flatten**。
- 等价性证明 A==B==C:A↔C(primary_action/blocking_reason)+ B↔C(actions primary=next_action、
primary+secondary 并集=allowed_actions),覆盖 `inventory_sufficiency`/`inventory_preparation`/
`artifact_generation`/`freight_upload_rpa` 四 Gate。
- **不可单独上线**的注意事项依旧:须与前端右卡仅读 A(§2.4)同批,否则右卡/对话流/批次进度中心会出现新漂移。
- [x] **后端(明细级按钮,§1.5 零 N+1 已实施 + 验证)**:`_build_stage_context`(`ui_state.py`)让
`preparation.lines[]` 携带 `line_allowed_actions`/`inventory_allowed_actions`/`inventory_file_no`/
`user_line_status`/`is_shortage`/`inventory_status`/`match_result`/`retryable`/`inventory_source` 等。
实现:在行循环内用**已加载的 `match_row` 内存列 + `detail_json`** 拼 fact,调用纯函数
`project_line_inventory_view` 结算,零逐行 SQL、不复用 Case `line_fulfillment` 重序列化器。
`release_required_fields`/`release_field_resolution_error`/`decision` 当前为 fail-closed 默认
(`[]`/`null`/`null`),避免为 release 补录字段引入逐行快照/模板/字段重查询(§5.1 禁止);后续独立路径解析后再覆盖。
N+1 守门:`test_preparation_line_allowed_actions_are_zero_n_plus_one`(6 行恒定数量级)。
- [x] i18n:四语言到齐,区分三库存动作文案。
(已完成:`workspace.json` 四语言 `actionCodes.*` 已区分
`retry_inventory_preparation`(重试库存准备/Retry inventory preparation/ลองเตรียมสต็อกใหม่)、
`retry_inventory_matching`(重试库存匹配/Retry inventory matching/ลองจับคู่สต็อกอีกครั้ง)、
`reload_inventory_result`(重新读取库存结果/Reload inventory result/อ่านผลสต็อกอีกครั้ง)。
并删除已下线动作的 4 语言残留键 `select_line_inventory_source`。JSON 校验通过 + 前端 376 用例全绿。)