feat(in-transit-transfer): 落地证据流重解析并纳入货代与排程配套改动

外部单号进入异动档自然键,圈档按 T-2 成熟封顶,当前归档可单独重解析。同批带上 Crane 禁整页刷新、ODM 白名单降级、排程抽屉与执行记录日期窗,以及 YAML 库存源下线。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-10-22 00:48:37 +08:00
1 parent 911afa0f73
commit 4c2a7e2a23
145 files changed
+11695 -4214

No files matched your search

@@ -7,6 +7,8 @@ scripts/freight_rpa/inbound/inbound_rpa_worker.py
逻辑:排程写入 inbound trigger → 本进程领取并按 connector/profile 分组串行导出 →
导出重试只覆盖落盘/校验(对齐库存);有表头无明细 → empty/no_inbound_data;
成功后先轻量 batch-progress 再 ready 归档回调;ready 超时不得重新下载。
批级失败(如 launch_persistent_context 浏览器已关)须对本组未终态单元逐笔
failed 回调,否则后端单元永久 awaiting_rpa、来源投影永久 fetching。
"""
from __future__ import annotations
@@ -459,6 +461,33 @@ def _report_failure(unit: dict[str, Any], exc: Exception) -> None:
_post_callback({'schema_version': 'freight_rpa.inbound.v1', 'callback_id': unit.get('callback_id'), 'status': 'failed', 'error_code': 'RPA.INBOUND_EXPORT_FAILED', 'error_message': str(exc)[:2000]})
def _report_group_open_failure(group: list[dict[str, Any]], exc: Exception) -> None:
"""批级浏览器打开/组级异常时,为组内全部单元补发 failed 回调。
用途:``_open_browser`` 在进入单元循环前失败时,原先只把 trigger 归档到 failed/
而不回调 → 后端单元永久 awaiting_rpa、来源选择卡永久「抓取中」且批量重试锁死
(须等 FREIGHT_RPA_INBOUND_TIMEOUT_SECONDS 孤儿回收)。本函数 best-effort 逐笔
``_report_failure``,单笔回调失败只打日志不中断其余。
Args:
group: 同一 connector/profile/login 下尚未进入终态回调的单元列表。
exc: 批级异常(通常为 Playwright TargetClosed / launch_persistent_context)。
"""
print(
f'[inbound-batch] open/group failed; reporting {len(group)} unit failure callback(s): '
f'{str(exc)[:200]}',
file=_sys.stderr,
)
for unit in group:
try:
_report_failure(unit, exc)
except Exception as callback_exc:
print(
f'[inbound-unit] failed callback failed source={unit.get("source_code")}: {callback_exc}',
file=_sys.stderr,
)
def _report_empty(unit: dict[str, Any], exc: NoInboundDataError) -> None:
"""无业务明细的合法结果:回调 empty + no_inbound_data(后端 finalize 计 skipped)。"""
print(f'[inbound-unit] empty source={unit.get("source_code")} callback={unit.get("callback_id")} message={str(exc)[:200]}')
@@ -606,6 +635,8 @@ def _process_units(body: dict[str, Any], drop_dir: Path) -> None:
**重试只覆盖导出/落盘/校验**(对齐库存 record_result 前段)。落盘成功后先轻量
``inbound-batch-progress``(管理端实时笔数),再调 ``inbound-batch-ready`` 归档;
ready/empty/failed 回调失败不得触发重新下载(2026-09-28 偶发同源双文件根因)。
批级异常(浏览器 launch 失败等)经 ``_report_group_open_failure`` 给未终态单元
补 failed 回调,禁止只归档 trigger 却让后端单元永久 awaiting_rpa。
"""
units = body.get('units')
if body.get('schema_version') != 'freight_rpa.inbound.v1' or body.get('action') != 'inbound_export_batch' or not isinstance(units, list) or not units:
@@ -623,6 +654,8 @@ def _process_units(body: dict[str, Any], drop_dir: Path) -> None:
mock = env('FREIGHT_RPA_WORKER_MOCK', 'false').lower() == 'true'
reuse_session = _session_reuse_enabled()
resources = (None, None, None, None)
# [STEP] 已终态回调的 callback_id:批级异常时只给剩余单元补 failed,避免双重终态。
reported_callback_ids: set[str] = set()
try:
if not mock:
resources = _open_browser(
@@ -694,6 +727,7 @@ def _process_units(body: dict[str, Any], drop_dir: Path) -> None:
f'[inbound-unit] empty callback failed source={unit.get("source_code")}: {callback_exc}',
file=_sys.stderr,
)
reported_callback_ids.add(str(unit.get('callback_id') or ''))
progress_counts['empty'] += 1
_emit_batch_progress(body, progress_counts)
continue
@@ -705,6 +739,7 @@ def _process_units(body: dict[str, Any], drop_dir: Path) -> None:
f'[inbound-unit] failed callback failed source={unit.get("source_code")}: {callback_exc}',
file=_sys.stderr,
)
reported_callback_ids.add(str(unit.get('callback_id') or ''))
progress_counts['failed'] += 1
_emit_batch_progress(body, progress_counts)
continue
@@ -726,6 +761,17 @@ def _process_units(body: dict[str, Any], drop_dir: Path) -> None:
f'file={output.name}: {callback_exc}',
file=_sys.stderr,
)
reported_callback_ids.add(str(unit.get('callback_id') or ''))
except Exception as exc:
# [STEP] 批级失败(浏览器未开 / 凭据上下文崩 / 未捕获逃逸):剩余单元补 failed,
# 否则 awaiting_rpa 永挂、前端「抓取中」锁死批量重试(2026-10-20 本地事故)。
remaining = [
unit for unit in group
if str(unit.get('callback_id') or '') not in reported_callback_ids
]
if remaining:
_report_group_open_failure(remaining, exc)
raise
finally:
playwright, context, _page, profile_dir = resources
if context is not None: