Files
syops/docs/superpowers/plans/2026-10-19-freight-rpa-class-prefix.md
T
hugoandCursor 39fe3c05f2 feat(freight-rpa): 库存/收料改名在手/入站,报表归属RPA,类名统一FreightRpa
把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与
枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa
持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀,
上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-19 10:46:06 +08:00

449 lines
26 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.
# 货代 RPA 类名前缀与上传动作 Implementation Plan
> **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:** 把表名已是 `freight_rpa_*` 的 ORM 类改成 `FreightRpa` 前缀,并把 Worker 上传动作与心跳能力改成 `freight_rpa_upload`。
**Architecture:** 两块互不依赖。任务 1 只改三个动作/能力字面量、原因码和两份现行文档,schema 仍是 `freight_rpa.upload.v1`。任务 2 用显式类名表做整词替换并移动模型文件,再只改站点关系属性 `inventory_source` 与 `receipt_report_config`。不留旧名别名,不写迁移。
**Tech Stack:** Flask、SQLAlchemy 2.0、pytest。Python 用 `backend/.venv/Scripts/python.exe`。pytest 的工作目录是 `backend/`。
**Spec:** `docs/superpowers/specs/2026-10-19-freight-rpa-class-prefix-design.md`
## Global Constraints
- 类名以 `FreightRpa` 开头。`ShippingInboundRpaUnitRun` 改为 `FreightRpaInboundUnitRun`,`ShippingTaskOnhandRpaAttempt` 改为 `FreightRpaOnhandTaskAttempt`,`ShippingOnhandSource` 改为 `FreightRpaSource`。
- 心跳能力与 trigger `action` 改为 `freight_rpa_upload`。`trigger_io.SCHEMA` 保持 `freight_rpa.upload.v1`。
- 不保留旧类名、旧模块路径、旧动作名的别名。后端与 Windows Worker 同一次发布。
- 不改任何表名、索引名、约束名、外键名。不新增 Alembic 迁移。`backend/migrations/` 不参与替换。
- 不改发货阶段 `freight_upload`、`freight_uploading`,不改门禁码 `freight_upload_rpa`,不改动作码 `retry_freight_upload_rpa`、`poll_freight_upload_rpa`、`trigger_freight_upload_rpa`,不改四语言文案。
- 函数名 `check_freight_upload_rpa_ready` 与 `assert_freight_upload_rpa_ready` 保留。
- 不改 `ShippingUploadAttempt` 与表 `shipping_notice_upload_attempts`。
- 不改这些发货类:`ShippingNotice`、`ShippingTaskOnhandLink`、`ShippingOnhandCandidateSet`、`ShippingOnhandCandidateSource`、`ShippingOnhandCandidateLine`、`ShippingOnhandPreparationAttempt`、`ShippingOnhandPreparationSourceAttempt`、`ShippingNoticeLineOnhandMatch`、`ShippingOnhandVerification`、`ShippingOnhandVerificationLine`、`ShippingFreightCancellation`、`ShippingExcelTemplate`。关系属性 `inventory_file` 保留。
- 不把履约出货改名为 outbound,不改 `shipment_*`。
- 不改环境变量 `FREIGHT_RPA_UPLOAD_*`,不改上传回调路径,不改 MinIO 键。
- 不改写 `docs/superpowers/plans/` 与 `docs/superpowers/specs/` 里已有计划正文。本规格的状态行除外,见任务 2 最后一步。
- 本计划覆盖 2026-10-17 对 `freight_rpa_upload` 的否决,也覆盖阶段 B「不改 `freight_upload`」里对动作名和心跳能力的那一句。阶段名和门禁码仍不改。
- 工作区里已有未提交的批次 S/D 与阶段 B。禁止 `git add -A`,禁止 `git commit`。每任务只暂存该任务列出的路径。
- 改签名或文件协作关系时同步文件头 docstring(AGENTS.md §5)。
## 文件地图
| 文件 | 职责 |
| --- | --- |
| `backend/app/services/freight_rpa/upload/trigger_io.py` | trigger 的 `action` 常量 |
| `backend/app/services/freight_rpa/common/worker_heartbeat.py` | 心跳能力常量、缺失原因码、管理端状态键 |
| `scripts/freight_rpa/common/rpa_heartbeat.py` | Windows Worker 上报的能力字符串 |
| `docs/features/freight-rpa/GLOSSARY.md` | Upload 条的现行动作名 |
| `docs/features/freight-rpa/cutover-runbook.md` | 发布窗口里的动作名,以及发布前清空 upload 目录 |
| `backend/app/models/freight_rpa_config.py` | 站点、登录连接、解析方案及在手配置类。由 `shipping_onhand_config.py` 改名 |
| `backend/app/models/freight_rpa_inbound_report.py` | 入站配置、执行单元、文件、通知。由 `shipping_inbound_report.py` 改名 |
| `backend/app/models/freight_rpa_onhand_mapping.py` | 在手字段映射。由 `shipping_onhand_mapping.py` 改名 |
| `backend/app/models/freight_rpa_onhand_task_attempts.py` | 在手任务抓取尝试。由 `shipping_onhand_task_rpa_attempts.py` 改名 |
| `backend/app/models/freight_rpa_onhand_report_file.py` | 在手报表文件。从 `shipping.py` 拆出 |
| `backend/app/models/__init__.py` | 导出新类名,不留旧类名 |
| `backend/tests/test_freight_rpa_class_names.py` | 锁表名、新类名、关系属性名 |
---
### Task 1: 上传动作与心跳能力
**Files:**
- Modify: `backend/app/services/freight_rpa/upload/trigger_io.py`
- Modify: `backend/app/services/freight_rpa/common/worker_heartbeat.py`
- Modify: `scripts/freight_rpa/common/rpa_heartbeat.py`
- Modify: `backend/tests/services/freight_rpa/test_upload_trigger_contract.py`
- Modify: `backend/tests/services/freight_rpa/test_worker_heartbeat.py`
- Modify: `backend/tests/test_freight_rpa_heartbeat.py`
- Modify: `docs/features/freight-rpa/GLOSSARY.md`
- Modify: `docs/features/freight-rpa/cutover-runbook.md`
- Test: `backend/tests/services/freight_rpa/test_upload_trigger_contract.py`
**Interfaces:**
- Consumes: 现有 `write_upload_trigger(...)`、`check_freight_upload_rpa_ready()`、`build_heartbeat_payload(...)`。函数名不改。
- Produces: `ACTION_FREIGHT_RPA_UPLOAD = "freight_rpa_upload"`;`FREIGHT_RPA_UPLOAD_CAPABILITY = "freight_rpa_upload"`;两者相等。缺失原因码 `missing_freight_rpa_upload_capability`。`SCHEMA` 仍为 `freight_rpa.upload.v1`。
- [ ] **Step 1: 把契约测试改成新动作名**
在 `backend/tests/services/freight_rpa/test_upload_trigger_contract.py` 把导入和断言换成:
```python
from app.services.freight_rpa.common.worker_heartbeat import FREIGHT_RPA_UPLOAD_CAPABILITY
from app.services.freight_rpa.upload import trigger_io
def test_upload_trigger_constants_align_with_heartbeat_capability():
"""upload action 必须与 Worker 心跳能力串 freight_rpa_upload 一致。"""
assert trigger_io.SCHEMA == "freight_rpa.upload.v1"
assert trigger_io.ACTION_FREIGHT_RPA_UPLOAD == "freight_rpa_upload"
assert trigger_io.ACTION_FREIGHT_RPA_UPLOAD == FREIGHT_RPA_UPLOAD_CAPABILITY
assert "outbound_upload" not in trigger_io.ACTION_FREIGHT_RPA_UPLOAD
assert "shipment_upload" not in trigger_io.ACTION_FREIGHT_RPA_UPLOAD
```
同一文件里 `test_write_upload_trigger_persists_freight_upload_action` 的两处动作断言改为:
```python
assert payload["action"] == trigger_io.ACTION_FREIGHT_RPA_UPLOAD
assert payload["schema_version"] == trigger_io.SCHEMA
assert payload["action"] == "freight_rpa_upload"
assert payload["schema_version"] == "freight_rpa.upload.v1"
```
在 `backend/tests/services/freight_rpa/test_worker_heartbeat.py` 把 `FREIGHT_UPLOAD_CAPABILITY` 的导入改成 `FREIGHT_RPA_UPLOAD_CAPABILITY`。`test_check_freight_upload_rpa_ready_requires_freight_upload_capability` 的断言改为:
```python
assert result.ready is False
assert any("missing_freight_rpa_upload_capability" in reason for reason in result.reasons)
```
`test_check_freight_upload_rpa_ready_when_worker_and_dirs_ok` 里心跳能力列表改为:
```python
capabilities=[ONHAND_EXPORT_CAPABILITY, FREIGHT_RPA_UPLOAD_CAPABILITY],
```
在 `backend/tests/test_freight_rpa_heartbeat.py` 的 `test_build_heartbeat_payload_inbound_worker_declares_inbound_export` 后面增加:
```python
def test_build_heartbeat_payload_adds_freight_rpa_upload_when_upload_enabled(monkeypatch):
"""开启上传时心跳能力是 freight_rpa_upload,不再上报 freight_upload。"""
module = _load_freight_rpa_heartbeat_module()
monkeypatch.setenv("FREIGHT_RPA_WORKER_UPLOAD_ENABLED", "true")
payload = module.build_heartbeat_payload(
trigger_dir=r"C:\HxopsRpaTest\data\onhand_triggers",
drop_dir=r"C:\HxopsRpaTest\data\onhand_drop",
)
assert payload["capabilities"] == ["onhand_export", "freight_rpa_upload"]
```
- [ ] **Step 2: 跑测试,确认失败**
工作目录 `backend/`:
```powershell
..\.venv\Scripts\python.exe -m pytest tests/services/freight_rpa/test_upload_trigger_contract.py tests/services/freight_rpa/test_worker_heartbeat.py::test_check_freight_upload_rpa_ready_requires_freight_upload_capability tests/test_freight_rpa_heartbeat.py::test_build_heartbeat_payload_adds_freight_rpa_upload_when_upload_enabled -q --tb=line
```
Expected: FAIL。导入失败或断言仍是 `freight_upload`。
- [ ] **Step 3: 改三个字面量**
`trigger_io.py`:
```python
SCHEMA = "freight_rpa.upload.v1"
ACTION_FREIGHT_RPA_UPLOAD = "freight_rpa_upload"
```
`write_upload_trigger` 写入的 `"action"` 改为 `ACTION_FREIGHT_RPA_UPLOAD`。文件头里描述动作的那一句改为:action 对齐心跳能力 `freight_rpa_upload`(`FREIGHT_RPA_UPLOAD_CAPABILITY`)。删除对常量名 `ACTION_FREIGHT_UPLOAD`、`FREIGHT_UPLOAD_CAPABILITY` 的引用。
`worker_heartbeat.py`:
```python
FREIGHT_RPA_UPLOAD_CAPABILITY = "freight_rpa_upload"
```
删除 `FREIGHT_UPLOAD_CAPABILITY`。所有引用改到新常量,包括 `check_freight_upload_rpa_ready` 与 `assert_freight_upload_rpa_ready` 内部。这两个函数名保留。
`_REASON_USER_HINTS` 的键改为 `missing_freight_rpa_upload_capability`,提示文案改为 `Worker 未声明 freight_rpa_upload 能力`。`_missing_capability_reason` 继续用 `f"missing_{required_capability}_capability"`,不要再手写旧码。
`serialize_rpa_worker_status` 返回字典里的键 `"freight_upload"` 改为 `"freight_rpa_upload"`。这是该文件里剩下的动作字面量;仓库里没有前端读取这个键。
文件头注释里「freight_upload 不变」改为「上传能力为 freight_rpa_upload」。
`scripts/freight_rpa/common/rpa_heartbeat.py` 的 `build_heartbeat_payload`:开启上传时 `capabilities.append("freight_rpa_upload")`。函数 docstring 同步写成 `freight_rpa_upload`。
- [ ] **Step 4: 改现行文档**
`docs/features/freight-rpa/GLOSSARY.md` Upload 条:心跳能力与上传 action 写成 `freight_rpa_upload`。schema 仍写 `freight_rpa.upload.v1`。保留「禁止 outbound_*」这句话。
`docs/features/freight-rpa/cutover-runbook.md` 里动作和心跳现在写成 `freight_upload` 或「`freight_upload` 不变」的地方,改成 `freight_rpa_upload`。在「一次上传」那一步之前加一句:发布前清空 upload 的 triggers 与 queue;已落盘且 `action` 为 `freight_upload` 的 trigger 不改写、不双读。
不要改 `docs/superpowers/plans/2026-10-17-freight-rpa-upload-action-fix.md`。
- [ ] **Step 5: 跑测试并检索旧字面量**
工作目录 `backend/`:
```powershell
..\.venv\Scripts\python.exe -m pytest tests/services/freight_rpa/test_upload_trigger_contract.py tests/services/freight_rpa/test_worker_heartbeat.py tests/test_freight_rpa_heartbeat.py -q --tb=short
```
Expected: PASS。
在仓库根目录检索。下面三条的结果必须为空:
```powershell
rg -n "ACTION_FREIGHT_UPLOAD|FREIGHT_UPLOAD_CAPABILITY" backend/app/services/freight_rpa/upload/trigger_io.py backend/app/services/freight_rpa/common/worker_heartbeat.py scripts/freight_rpa/common/rpa_heartbeat.py
rg -n "\"freight_upload\"" backend/app/services/freight_rpa/upload/trigger_io.py backend/app/services/freight_rpa/common/worker_heartbeat.py scripts/freight_rpa/common/rpa_heartbeat.py
rg -n "missing_freight_upload_capability" backend/app/services/freight_rpa/common/worker_heartbeat.py backend/tests/services/freight_rpa/test_worker_heartbeat.py
```
`check_freight_upload_rpa_ready` 仍在 `worker_heartbeat.py`。发货阶段文件里的 `freight_upload` 不动。
- [ ] **Step 6: 只暂存本任务路径**
```powershell
git add -- backend/app/services/freight_rpa/upload/trigger_io.py backend/app/services/freight_rpa/common/worker_heartbeat.py scripts/freight_rpa/common/rpa_heartbeat.py backend/tests/services/freight_rpa/test_upload_trigger_contract.py backend/tests/services/freight_rpa/test_worker_heartbeat.py backend/tests/test_freight_rpa_heartbeat.py docs/features/freight-rpa/GLOSSARY.md docs/features/freight-rpa/cutover-runbook.md
```
---
### Task 2: ORM 类名与模型文件
**Files:**
- Create: `backend/app/models/freight_rpa_config.py`
- Create: `backend/app/models/freight_rpa_inbound_report.py`
- Create: `backend/app/models/freight_rpa_onhand_mapping.py`
- Create: `backend/app/models/freight_rpa_onhand_task_attempts.py`
- Create: `backend/app/models/freight_rpa_onhand_report_file.py`
- Create: `backend/tests/test_freight_rpa_class_names.py`
- Modify: `backend/app/models/shipping.py`
- Modify: `backend/app/models/__init__.py`
- Delete: `backend/app/models/shipping_onhand_config.py`
- Delete: `backend/app/models/shipping_inbound_report.py`
- Delete: `backend/app/models/shipping_onhand_mapping.py`
- Delete: `backend/app/models/shipping_onhand_task_rpa_attempts.py`
- Test: `backend/tests/test_freight_rpa_class_names.py`
引用这些类的 `backend/app`、`backend/tests`、`backend/scripts` 由下面的脚本改名。`backend/migrations/` 与 `docs/superpowers/` 不跑脚本。
**Interfaces:**
- Consumes: 任务 1 不需要。表名保持规格第 3 节那一列。
- Produces: `from app.models import FreightRpaSource, FreightRpaOnhandReportFile, FreightRpaInboundReportFile, FreightRpaInboundUnitRun, FreightRpaParsingProfile, FreightRpaProfile`。`FreightRpaSource.inbound_report_config`。`FreightRpaOnhandReportConfig.source`、`FreightRpaOnhandExternalMapping.source`、`FreightRpaInboundReportConfig.source`、`FreightRpaInboundUnitRun.source`。`app.models` 上不再有 `ShippingOnhandSource`、`ShippingOnhandFile`、`ShippingInboundReportFile`、`ShippingParsingProfile`。
- [ ] **Step 1: 写会失败的类名测试**
创建 `backend/tests/test_freight_rpa_class_names.py`:
```python
"""
backend/tests/test_freight_rpa_class_names.py
用途:锁住货代 RPA 模型的类名、表名和站点关系属性名。
依赖:app.models 里拆出的 freight_rpa_* 模型模块。
逻辑:被 pytest 直接导入;不连数据库。
"""
from __future__ import annotations
import app.models as models
from app.models.freight_rpa_config import (
FreightRpaOnhandExternalMapping,
FreightRpaOnhandReportConfig,
FreightRpaSource,
)
from app.models.freight_rpa_inbound_report import (
FreightRpaInboundReportConfig,
FreightRpaInboundReportFile,
FreightRpaInboundUnitRun,
)
from app.models.freight_rpa_onhand_report_file import FreightRpaOnhandReportFile
from app.models.freight_rpa_onhand_task_attempts import FreightRpaOnhandTaskAttempt
def test_freight_rpa_classes_keep_existing_table_names():
"""类名换成 FreightRpa 后,表名仍是规格里的 freight_rpa_*。"""
assert FreightRpaSource.__tablename__ == "freight_rpa_sources"
assert FreightRpaOnhandReportFile.__tablename__ == "freight_rpa_onhand_report_files"
assert FreightRpaInboundReportFile.__tablename__ == "freight_rpa_inbound_report_files"
assert FreightRpaInboundUnitRun.__tablename__ == "freight_rpa_inbound_rpa_unit_runs"
def test_site_relationships_drop_inventory_and_receipt_names():
"""站点关系改叫 source / inbound_report_config,索引名不在这个测试里改。"""
assert "source" in FreightRpaOnhandReportConfig.__mapper__.relationships
assert "inventory_source" not in FreightRpaOnhandReportConfig.__mapper__.relationships
assert "source" in FreightRpaInboundReportConfig.__mapper__.relationships
assert "source" in FreightRpaInboundUnitRun.__mapper__.relationships
assert "inbound_report_config" in FreightRpaSource.__mapper__.relationships
assert "receipt_report_config" not in FreightRpaSource.__mapper__.relationships
assert "source" in FreightRpaOnhandExternalMapping.__mapper__.relationships
assert FreightRpaOnhandTaskAttempt.__tablename__ == "freight_rpa_onhand_task_rpa_attempts"
def test_old_shipping_class_names_are_not_exported():
"""旧类名不留别名。发货任务关联类仍叫 ShippingTaskOnhandLink。"""
assert not hasattr(models, "ShippingOnhandSource")
assert not hasattr(models, "ShippingOnhandFile")
assert not hasattr(models, "ShippingInboundReportFile")
assert not hasattr(models, "ShippingParsingProfile")
assert hasattr(models, "FreightRpaSource")
assert hasattr(models, "FreightRpaOnhandReportFile")
assert hasattr(models, "ShippingTaskOnhandLink")
assert hasattr(models, "ShippingUploadAttempt")
```
- [ ] **Step 2: 跑测试,确认失败**
工作目录 `backend/`:
```powershell
..\.venv\Scripts\python.exe -m pytest tests/test_freight_rpa_class_names.py -q --tb=line
```
Expected: FAIL,模块 `app.models.freight_rpa_config` 不存在。
- [ ] **Step 3: 移动文件并按显式表替换类名**
在仓库根目录把下面整段存成临时文件 `rename_freight_rpa_classes.py` 并执行。跑完删除它,不要 `git add` 它。脚本先 `git mv`,再替换模块路径,再只替换类名整词,长名字在前。不要把 `ShippingOnhand` 当成前缀去替换。
```python
import re
import subprocess
from pathlib import Path
ROOT = Path.cwd()
MOVES = [
("backend/app/models/shipping_onhand_config.py", "backend/app/models/freight_rpa_config.py"),
("backend/app/models/shipping_inbound_report.py", "backend/app/models/freight_rpa_inbound_report.py"),
("backend/app/models/shipping_onhand_mapping.py", "backend/app/models/freight_rpa_onhand_mapping.py"),
("backend/app/models/shipping_onhand_task_rpa_attempts.py", "backend/app/models/freight_rpa_onhand_task_attempts.py"),
]
MODULE_PATHS = [
("app.models.shipping_onhand_task_rpa_attempts", "app.models.freight_rpa_onhand_task_attempts"),
("app.models.shipping_onhand_config", "app.models.freight_rpa_config"),
("app.models.shipping_inbound_report", "app.models.freight_rpa_inbound_report"),
("app.models.shipping_onhand_mapping", "app.models.freight_rpa_onhand_mapping"),
("models.shipping_onhand_task_rpa_attempts", "models.freight_rpa_onhand_task_attempts"),
("models.shipping_onhand_config", "models.freight_rpa_config"),
("models.shipping_inbound_report", "models.freight_rpa_inbound_report"),
("models.shipping_onhand_mapping", "models.freight_rpa_onhand_mapping"),
]
RENAMES = [
("ShippingOnhandSourceParsingBindingAudit", "FreightRpaSourceParsingBindingAudit"),
("ShippingOnhandRoutingRuleCondition", "FreightRpaOnhandRoutingRuleCondition"),
("ShippingOnhandRoutingRuleVersion", "FreightRpaOnhandRoutingRuleVersion"),
("ShippingOnhandConfigurationTestRun", "FreightRpaOnhandConfigurationTestRun"),
("ShippingInboundNotificationDelivery", "FreightRpaInboundNotificationDelivery"),
("ShippingOnhandStructureAnalysis", "FreightRpaOnhandStructureAnalysis"),
("ShippingOnhandExternalMapping", "FreightRpaOnhandExternalMapping"),
("ShippingOnhandValidationBatch", "FreightRpaOnhandValidationBatch"),
("ShippingInboundReportConfig", "FreightRpaInboundReportConfig"),
("ShippingOnhandReportConfig", "FreightRpaOnhandReportConfig"),
("ShippingInboundReportFile", "FreightRpaInboundReportFile"),
("ShippingTaskOnhandRpaAttempt", "FreightRpaOnhandTaskAttempt"),
("ShippingOnhandRoutingRule", "FreightRpaOnhandRoutingRule"),
("ShippingInboundRpaUnitRun", "FreightRpaInboundUnitRun"),
("ShippingParsingProfile", "FreightRpaParsingProfile"),
("ShippingOnhandMapping", "FreightRpaOnhandMapping"),
("ShippingOnhandSource", "FreightRpaSource"),
("ShippingOnhandField", "FreightRpaOnhandField"),
("ShippingOnhandFile", "FreightRpaOnhandReportFile"),
("ShippingRpaProfile", "FreightRpaProfile"),
]
SKIP_PARTS = {"migrations", "superpowers", ".superpowers"}
ROOTS = [
ROOT / "backend" / "app",
ROOT / "backend" / "tests",
ROOT / "backend" / "scripts",
ROOT / "scripts" / "freight_rpa",
]
for src, dst in MOVES:
subprocess.run(["git", "mv", src, dst], check=True, cwd=ROOT)
for base in ROOTS:
for path in base.rglob("*"):
if not path.is_file() or path.suffix not in {".py", ".md"}:
continue
if any(part in SKIP_PARTS for part in path.parts):
continue
text = path.read_text(encoding="utf-8")
updated = text
for old, new in MODULE_PATHS:
updated = updated.replace(old, new)
for old, new in RENAMES:
updated = re.sub(rf"\b{old}\b", new, updated)
if updated != text:
path.write_text(updated, encoding="utf-8")
```
然后把 `FreightRpaOnhandReportFile` 的整个类(含它上方的类 docstring,不含 `ShippingTaskOnhandLink`)从 `backend/app/models/shipping.py` 剪到新文件 `backend/app/models/freight_rpa_onhand_report_file.py`。新文件自己 import `Base` 以及这个类用到的 SQLAlchemy 符号。`shipping.py` 若还引用该类,改为从新模块导入,或只在类型注解里用字符串 `"FreightRpaOnhandReportFile"`。`ShippingTaskOnhandLink.inventory_file` 的目标类字符串改为 `"FreightRpaOnhandReportFile"`,属性名 `inventory_file` 不变。
把新文件头写成:
```python
"""
backend/app/models/freight_rpa_onhand_report_file.py
用途:货代 RPA 抓回的在手报表文件。
依赖:models.base.Base、shipping.py 里的 ShippingTaskOnhandLink。
逻辑:表名 freight_rpa_onhand_report_files 不变;发货任务通过 ShippingTaskOnhandLink 引用本行。
"""
```
另外四个模型文件的文件头第一行改成新路径,类名改成新类名。`__tablename__`、`Index(...)`、`UniqueConstraint(...)`、`CheckConstraint(...)` 的字符串一个字都不改。
`backend/app/models/__init__.py` 的 import 路径改到新模块,`__all__` 只导出新类名。文件头「在手/入站模型只导出 ShippingOnhand* 与 ShippingInbound*」改为「在手/入站模型导出 FreightRpa*」。
- [ ] **Step 4: 只改两个关系属性**
不要全局把 `inventory_source` 换成 `source`。投影字典的键 `'inventory_source'`、门禁码、`inventory_source_code` 都要留下。
只做这三类替换,范围是 `backend/app`、`backend/tests`、`backend/scripts`:
1. 模型字段行 `inventory_source:` 改为 `source:`。出现位置是 `FreightRpaOnhandReportConfig`、`FreightRpaOnhandExternalMapping`、`FreightRpaInboundReportConfig`、`FreightRpaInboundUnitRun`。
2. 模型文件里的 `back_populates='inventory_source'` 改为 `back_populates='source'`。
3. 关键字实参 `inventory_source=` 改为 `source=`。点号访问 `.inventory_source` 改为 `.source`。已知位置包括 `shipping_onhand_admin_service.py` 构造 `FreightRpaOnhandReportConfig`、`onhand_configuration_test.py`、`import_freight_rpa_sources.py`、`freight_rpa_inbound_job.py` 的 `unit.inventory_source`、`api/schedule/runs.py` 的 `retry.inventory_source`。
`receipt_report_config` 用整词替换成 `inbound_report_config`,同一范围。这个整词不会打中 `get_receipt_report_config`,因为中间有下划线。不要改 `backend/migrations` 里的旧表名 `shipping_receipt_report_configs`。
改完后这两条结果必须为空:
```powershell
rg -n "\.inventory_source\b|inventory_source=" backend/app backend/tests backend/scripts
rg -n "\breceipt_report_config\b" backend/app backend/tests backend/scripts
```
`rg -n "inventory_source" backend/app/services/shipping_notice/line_onhand_match_projection.py` 仍应能看到字典键 `'inventory_source'`。那是发货投影,本任务不改。
- [ ] **Step 5: 跑类名测试并检索旧类名**
工作目录 `backend/`:
```powershell
..\.venv\Scripts\python.exe -m pytest tests/test_freight_rpa_class_names.py -q --tb=short
```
Expected: PASS。
仓库根目录,下面每一条结果必须为空。把规格第 3 节的旧类名逐个放进同一条正则:
```powershell
rg -n "\bShippingOnhandSource\b|\bShippingOnhandFile\b|\bShippingInboundReportFile\b|\bShippingInboundRpaUnitRun\b|\bShippingTaskOnhandRpaAttempt\b|\bShippingParsingProfile\b|\bShippingRpaProfile\b|\bShippingOnhandReportConfig\b|\bShippingOnhandMapping\b|\bShippingInboundReportConfig\b|\bShippingInboundNotificationDelivery\b|\bShippingOnhandField\b|\bShippingOnhandExternalMapping\b|\bShippingOnhandStructureAnalysis\b|\bShippingOnhandConfigurationTestRun\b|\bShippingOnhandValidationBatch\b|\bShippingOnhandRoutingRule\b|\bShippingOnhandRoutingRuleCondition\b|\bShippingOnhandRoutingRuleVersion\b|\bShippingOnhandSourceParsingBindingAudit\b" backend/app scripts/freight_rpa
```
`backend/migrations` 里的旧类名允许留下。
旧模块路径也必须为空:
```powershell
rg -n "models\.shipping_onhand_config|models\.shipping_inbound_report|models\.shipping_onhand_mapping|models\.shipping_onhand_task_rpa_attempts|shipping import FreightRpaOnhandReportFile" backend/app backend/tests backend/scripts
```
若 `from app.models.shipping import FreightRpaOnhandReportFile` 仍在,改成 `from app.models.freight_rpa_onhand_report_file import FreightRpaOnhandReportFile`,或改成 `from app.models import FreightRpaOnhandReportFile`。
再跑一条导入检查,确认模型包能加载:
```powershell
..\.venv\Scripts\python.exe -c "import app.models as m; assert m.FreightRpaInboundReportFile.__tablename__=='freight_rpa_inbound_report_files'"
```
工作目录仍是 `backend/`。
- [ ] **Step 6: 改规格状态行**
`docs/superpowers/specs/2026-10-19-freight-rpa-class-prefix-design.md` 第一段状态从 `待评审` 改为 `已有实施计划`,并写上本计划路径 `docs/superpowers/plans/2026-10-19-freight-rpa-class-prefix.md`。规格正文的类名对照表不要改。
- [ ] **Step 7: 只暂存本任务碰到的路径**
先删掉临时脚本 `rename_freight_rpa_classes.py`。然后暂存新模型文件、测试、`shipping.py`、`models/__init__.py`,以及 `git status --short` 里确因本任务改了类名或关系属性的 `backend/app`、`backend/tests`、`backend/scripts` 文件。不要暂存 `backend/migrations`。不要 `git add -A`。规格状态行一并暂存:
```powershell
git add -- docs/superpowers/specs/2026-10-19-freight-rpa-class-prefix-design.md backend/tests/test_freight_rpa_class_names.py backend/app/models/freight_rpa_config.py backend/app/models/freight_rpa_inbound_report.py backend/app/models/freight_rpa_onhand_mapping.py backend/app/models/freight_rpa_onhand_task_attempts.py backend/app/models/freight_rpa_onhand_report_file.py backend/app/models/shipping.py backend/app/models/__init__.py
```
其余被类名替换碰到的文件,按 `git diff --name-only` 逐个 `git add -- <path>`。