# 货代 Gate 编排对齐 — 匹配后独立 Excel Attempt 实现计划 > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. **Goal:** 把履约段(Gate 13–20)统一成「当前 Gate Attempt 只做本门禁职责 → `complete` → 入队下一 Gate Attempt」;拆除匹配 Attempt 内嵌的 `try_auto_chain` Excel/上传副作用,使 `artifact_generation` 成为可观测的独立 Attempt。 **Architecture:** 保留既有 P0-3 脊柱:`submit_auto_progress` / `_enqueue_auto_progress_after_gate_pass` → `queue_gate_attempt` → Job Worker → `execute_queued_attempt` → `complete_gate_attempt`。匹配 Handler 只写匹配事实并 `sync`;Excel 仅由 `_handle_regenerate_excel` 在 `artifact_generation` Attempt 内、经 `_task_payload`(`_orchestrator_internal_dispatch=True`)调用 `generate_excel`。事实门禁(sufficiency / excel_readiness / mapping)允许 sync 投影,但 **不得**在 sync 里把「尚未执行的 worker 门禁」标成已完成产物工作。 **Tech Stack:** Python 3.12 + Flask + SQLAlchemy 2.0;测试 `backend\.venv\Scripts\python.exe -m pytest`;文档 `docs/features/shipping-notice/process-gate-recovery-contract.md` / `technical.md`。 ## Global Constraints - 分支现状:在现有 shipping 开发分支上改;**不自动 push**;用户未明确要求时不 commit。 - 后端解释器:`backend\.venv\Scripts\python.exe`;只跑受影响测试文件。 - **禁止**再引入新的跨 Gate 内嵌工作流;`_orchestrator_internal_dispatch` 仅表示「已在本 Attempt 内直跑本 Gate 的 workflow」,不得用来在匹配 Attempt 里生成 Excel。 - 配置语义:`SHIPPING_NOTICE_AUTO_GENERATE_EXCEL_ENABLED` 控制「是否自动入队 artifact Attempt」,**不再**表示「在 match 里直跑 generate」。 - `SHIPPING_NOTICE_AUTO_UPLOAD_ENABLED` 同理:只控制是否自动入队 freight Attempt,不在 match 内嵌 upload。 - 同步更新契约/技术文档中与自动串联相关的段落,避免文档继续描述嵌套链。 --- ## 现状审计结论(实施前共识) ### 正式脊柱(应对齐的好模式) ```text submit_auto_progress / complete(passed) → _enqueue_auto_progress_after_gate_pass → queue_gate_attempt + enqueue_gate_attempt_job → Worker claim_and_run_gate_attempt → execute_queued_attempt → handler(_task_payload + internal) → complete_gate_attempt → 再入队下一 Gate ``` 关键落点: | 角色 | 文件 | 函数 | | --- | --- | --- | | 任务级提交 | `auto_progress_controller.py` | `submit_auto_progress` | | Attempt 收口续跑 | `process_gate_service.py` | `complete_gate_attempt` → `_enqueue_auto_progress_after_gate_pass` | | Worker | `process_gate_job_service.py` / `jobs/handlers/shipping_process_gate.py` | `claim_and_run_gate_attempt` | | 分发 | `process_gate_executor.py` | `execute_queued_attempt` / `_task_payload` | | 事实投影 | `process_gate_bridge.py` | `sync_fulfillment_gates_from_task` | ### Gate 13–20 分类(今天) | Gate | 好模式? | 今天主路径 | | ---: | --- | --- | | 13 preparation | ✅ | **新 Attempt** 跑 `ensure_inventory`;不内嵌 match/excel | | 14 field_mapping | ⚪ 事实门禁 | 多为 **sync 投影**;恢复路径 `from_field_mapping` **同 Attempt 嵌套 match**(次要债务) | | 15 matching | ❌ | **新 Attempt** 跑 match,但成功后 **`try_auto_chain` 同 Attempt 内嵌 generate±upload** | | 16 sufficiency | ⚪ 事实/人工 | sync / recheck;短缺则 **人工** | | 17 excel_readiness | ✅ 事实门禁 | sync / recheck **不生成** Excel | | 18 artifact | ❌ 契约有、开心路径绕过 | 自动成功靠 match 内嵌;手动「重新生成」才走 `_handle_regenerate_excel` **新 Attempt** | | 19 freight RPA | 部分 | 有独立 Handler;`AUTO_UPLOAD` 时可被 chain 嵌套 | | 20 manual handoff | ✅ | **owner 人工**;auto enqueue 跳过 | ### 目标开心路径 ```text Attempt(prep) → pass → enqueue Attempt(match) → 仅匹配 + sync 事实 → pass → enqueue [可选 Attempt/sync excel_readiness] Attempt(artifact) → generate_excel(_task_payload) → pass → enqueue Attempt(freight) 或 停在 manual_upload(人工) ``` --- ## File map | 文件 | 职责 | | --- | --- | | `fulfillment_workflow.py` | 收缩/拆分 `try_auto_chain_after_match`;match 成功不再 generate/upload | | `process_gate_executor.py` | 确认 match handler 只 sync+return;artifact handler 保持原子 | | `process_gate_bridge.py` | sync:无 Artifact 时 **不得**把 artifact 标 passed;可保留 readiness 资格投影 | | `process_gate_service.py` | 确认 match pass 后续跑能落到 `regenerate_excel_artifact` | | `auto_progress_controller.py` | 确认 technical next_action 覆盖 artifact | | `process_gate_contract.py` | 如需:明确 artifact 自动 next_action / 配置开关语义 | | `docs/features/shipping-notice/process-gate-recovery-contract.md` | 记录「禁止跨 Gate 内嵌产物」 | | `docs/features/shipping-notice/technical.md` | 删改 `try_auto_chain` 开心路径描述 | | 测试 | STP acceptance、auto_progress、process recovery、line rematch、excel regenerate | --- ### Task 1: 红灯 — 匹配 Attempt 不得生成 Excel;artifact 须有独立 Attempt **Files:** - Create/Modify: `backend/tests/test_shipping_gate_orchestration_separation.py`(新建聚焦文件) - 可参考:`test_shipping_notice_stp_acceptance.py`、`test_shipping_auto_progress_p0_3.py`、`test_shipping_process_recovery_plan.py` - [x] **Step 1–3: 红灯测试已写入并随实现转绿**(见 `test_shipping_gate_orchestration_separation.py`) - [ ] **Step 4: 本地 commit(仅当用户要求)** --- ### Task 2: 拆除 match 内嵌 `try_auto_chain` 产物副作用 **Files:** - Modify: `backend/app/services/shipping_notice/fulfillment_workflow.py` - Modify: `backend/tests/test_shipping_notice_stp_acceptance.py`(改写旧「chain 必调 generate」断言) - [x] **Step 1: 收缩 `try_auto_chain_after_match`** - [x] **Step 2: 更新 STP acceptance 测试**(Gate 路径不调 generate;遗留路径只 submit auto_progress) - [x] **Step 3: 跑聚焦测试**(orchestration separation 全绿;STP chain 相关用例绿) --- ### Task 3: 保证 match pass 后续跑落到独立 `artifact_generation` Attempt **Files:** - Modify: `backend/app/services/shipping_notice/process_gate_bridge.py`(必要时) - Modify: `backend/app/services/shipping_notice/process_gate_service.py` / `process_gate_contract.py`(若 next_action 未自动露出) - Modify: `backend/app/services/shipping_notice/process_gate_executor.py`(仅确认 `_handle_regenerate_excel` 不变) - [x] **Step 1: sync 规则收紧**(STEP 6A 无 export 不得 passed;保留 regenerate next_action) - [x] **Step 2: 确认 enqueue 不跳过 artifact**(`_auto_generate_excel_enabled` 门控入队) - [x] **Step 3: 绿灯 Task1 入队断言** - [x] **Step 4: recovery / auto_progress_p0_3 聚焦回归通过** --- ### Task 4: 端到端编排回归(prep → match → artifact → handoff) **Files:** - Modify/Create: `backend/tests/test_shipping_gate_orchestration_separation.py` 增加集成用例 - 可选:轻量 spy Job enqueue,避免真实 RPA - [x] **Step 1: 集成断言**(`test_happy_path_generate_excel_only_in_artifact_attempt`) - [x] **Step 2: 跑测试至全绿** --- ### Task 5: 次要债务(可同 PR 或 follow-up)— 恢复路径嵌套 **Files:** - Modify: `process_gate_executor.py` → `_handle_retry_inventory_from_field_mapping` - [x] **Step 1: 评估范围** — **Follow-up P1**(本迭代不拆):映射恢复 Attempt 仍嵌套 `_handle_retry_inventory_matching`;函数注释已标注目标形态。 - [ ] **Step 2: 若本迭代纳入** — 延期。 --- ### Task 6: 文档与死代码标注 **Files:** - Modify: `docs/features/shipping-notice/process-gate-recovery-contract.md` - Modify: `docs/features/shipping-notice/technical.md`(`try_auto_chain` / auto excel 相关) - Modify: `progress_workflow.py` 文件头注释(标明 P0-3 后旧跨 Gate 体为 dead) - [x] **Step 1: 契约增补一条硬规则** - [x] **Step 2: technical.md 开心路径改为「match pass → enqueue artifact Attempt」** - [x] **Step 3: 可选清理** — `_dispatch_legacy_orchestration` / STEP 3 注释标 DEPRECATED。 --- ### Task 7: 验收清单(人工) - [ ] 重匹配成功后:诊断/Attempt 列表出现 **独立的** `artifact_generation` Attempt(不仅是 matching)。 - [ ] 右卡在生成中短暂停留「Excel 生成」而非匹配完成后瞬间跳过。 - [ ] 生成失败:仅 artifact Attempt `blocked_retryable` +「重新生成 Excel」;matching 保持 passed。 - [ ] `AUTO_GENERATE_EXCEL=false`:匹配通过后停在可点「重新生成」,不静默 noop。 - [ ] `AUTO_UPLOAD=true`:artifact pass 后再出现 freight Attempt(不在 match 内嵌 upload)。 - [ ] i18n / `can_fulfill_complete_line` 等已合入改动回归仍正常。 --- ## 风险与回滚 | 风险 | 缓解 | | --- | --- | | 匹配通过后若 enqueue 失败会停在无 Excel | 测试锁 enqueue;失败时 UI 仍有 regenerate | | 延迟增加(多一个 Job) | 可接受;观测性优先 | | 旧 chat tool 仍调 `try_auto_chain` | Gate 路径切断;遗留路径改为只 submit auto_progress | | sync 与 enqueue 竞态 | 沿用现有 Job 去重 / idempotency_key;禁止 sync 提前 passed artifact | 回滚:恢复 `try_auto_chain` 内 generate(仅应急);正式修复应保留独立 Attempt。 --- ## 建议实施顺序 1. Task 1 红灯 2. Task 2 拆 chain 3. Task 3 sync + enqueue 4. Task 4 集成 5. Task 6 文档 6. Task 5 视排期 7. Task 7 人工验收 **非目标(本计划不做):** 重写 20 Gate 模型、改前端按钮契约、合并 excel_readiness 与 artifact、删除全部 legacy auto_progress 函数体。