fix(shipping-notice): 重识别 Run 终态激活 Case 并收紧裸数字兜底口径

- 修复重解析延迟激活钩子未接线:Run 终态原子激活可复核 Case 为文件 Stream 当前版本,工作台队列不再空白
- 新增 run_is_reparse_successor_of_case 判定 Case 与重解析 Run 的后继关系
- 裸数字兜底口径收紧:仅扫「整格恰好 5/10 位纯数字」的正文 HTML 表格单元格,自由文本与带前后字符的值一律不取,防误绑签名与邮编
- 新增客户 PO 匹配变体集合(原值/归一/去前导零)纳入排除集,补充合并末段裁决更准
- 同步预约号启发式测试,business/technical 文档记录收紧决策
This commit is contained in:
hugo committed 2026-10-15 01:26:23 +08:00
1 parent 1efb165fd5
commit e78f750db0
9 files changed
+208 -98

No files matched your search

@@ -5,8 +5,9 @@ services/shipping_notice/reservation_id.py
依赖:正则表达式与标准 `typing` 工具;裸数字兜底扫描惰性依赖版面 HTML 网格解析。
逻辑:由 notice_lines、inventory_snapshot、match、extract、EML 补充解析等多个模块复用;
归一结果恒为 5/10 位纯数字(可剥括号备注、@ 前缀、短数字行序后缀如 35354-1)。
另提供「裸数字兜底」:正文无表头且常规证据缺失时,扫描排除后的 5/10 位
纯数字候选(供 order_supplement_merge 在合并末段做唯一/多候选裁决)。
另提供「裸数字兜底」:正文数据表无表头且常规证据缺失时,扫描「整格恰好为
5/10 位纯数字」的 HTML 表格单元格候选(供 order_supplement_merge 在合并
末段做唯一/多候选裁决;自由文本不扫描)。
"""
from __future__ import annotations
@@ -32,11 +33,10 @@ PR_NUMBER_LINE_RE = re.compile(
r"PR\s*Number\s*[::]?\s*([^\n\r]+)",
re.IGNORECASE,
)
# [裸数字兜底] 独立的 5/10 位纯数字 token;前后不能紧贴数字,防止从更长数字串截取。
BARE_NUMERIC_RESERVATION_RE = re.compile(r"(?<!\d)(\d{5}|\d{10})(?!\d)")
# [裸数字兜底] 紧跟业务标签的数字串(PO/订单/发票/数量)不是预约单号;捕获组为数字本体。
# 保留前导词边界防止命中复合词内部;尾部不加边界、由「必须跟数字」门控,
# 使 PO12345 / PO# 12345 / Purchase Order: 12345 等写法都被覆盖,而 podium 等词不受影响。
# 仅用于邮件主题等上下文的标签排除;正文候选已收紧为「整格纯数字」,不再做自由文本扫描。
LABELED_NON_RESERVATION_RE = re.compile(
r"(?i)(?:\bp\.?\s*o\.?|\bpurchase\s+orders?|\borders?|\binvoices?|\binv\.?|\bqty\.?|\bquantity)"
r"(?:\s*(?:number|no\.?|#))?\s*[:#]?\s*(\d{3,})"
@@ -184,13 +184,6 @@ def reservation_ids_in_labeled_context(text: str) -> set[str]:
}
def _heuristic_context_snippet(text: str, match: re.Match[str]) -> str:
"""截取 token 所在行命中国的前后片段,供复核面板展示证据上下文。"""
start = max(match.start() - 40, 0)
end = min(match.end() + 40, len(text))
return text[start:end].strip()
def _iter_heuristic_cell_texts(html_tables: list[str] | None, content_hash: str):
"""惰性产出 HTML 表格单元格文本及其证据定位;解析失败的表安全跳过。
@@ -220,54 +213,49 @@ def _iter_heuristic_cell_texts(html_tables: list[str] | None, content_hash: str)
}
def _heuristic_cell_is_bare_number(cell_text: str) -> bool:
"""判断表格单元格是否「整格恰好为 5/10 位纯数字」(2026-10-15 收紧规则)。
前后带任何字符都不取:地址邮编(``Secaucus, NJ 07094``)、带字母前后的
编号(``T00410``)、带备注的值(``39739 (A)``)一律排除,只留独立数值格。
"""
return bool(RESERVATION_ID_NUMERIC_RE.fullmatch(str(cell_text or '').strip()))
def find_bare_numeric_reservation_candidates(
*,
body_text: str = "",
html_tables: list[str] | None = None,
content_hash: str = "",
) -> list[dict[str, Any]]:
"""扫描正文中的裸 5/10 位纯数字预约单号候选(排除带业务标签的数字)。
"""扫描正文 HTML 表格中「整格恰好为 5/10 位纯数字」的预约单号候选。
AI 触发时机:EML 正文数据行没有表头、常规表头契约解析不出预约单号时,
AI 触发时机:EML 正文数据表没有表头、常规表头契约解析不出预约单号时,
在补充事实里预置候选列表;真正「是否采纳」由 order_supplement_merge 在
合并末段结合已提取字段值裁决,本函数绝不直接写入订单。
核心逻辑:先扫 HTML 表格单元格(保留 DOM cell_id 证据),再扫纯文本行
(保留行号与上下文片段);紧跟 PO/订单/发票/数量标签的数字串不作为候选;
同值只保留首个证据;总数截断到 MAX_HEURISTIC_RESERVATION_CANDIDATES。
核心逻辑:只认表格(2026-10-15 收紧)——自由文本(签名、地址、引用链)
一律不扫描,杜绝邮编等噪声;单元格整格纯数字才算候选(PO 号同为 10 位
纯数字会入选,由合并端按已提取字段值/主题标签排除);同值只保留首个证据;
总数截断到 MAX_HEURISTIC_RESERVATION_CANDIDATES。
Args:
body_text: EML 正文纯文本(含展平的数据行)。
html_tables: 正文顶层 HTML 表格片段列表;可为空。
html_tables: 正文顶层 HTML 表格片段列表;为空时返回空列表(无表格不兜底)。
content_hash: 正文 Document 内容哈希,用于稳定 cell_id。
Returns:
list[dict]: ``[{value, evidence}]``;evidence 含 table_index/row_index/
column_index/cell_id/raw_value,纯文本候选另含 context。扫描不到返回空列表。
column_index/cell_id/raw_value(DOM 级可追溯)。
"""
candidates: list[dict[str, Any]] = []
seen: set[str] = set()
def _collect(text: str, base_evidence: dict[str, Any]) -> None:
excluded_spans = [item.span() for item in LABELED_NON_RESERVATION_RE.finditer(text)]
for match in BARE_NUMERIC_RESERVATION_RE.finditer(text):
if any(start <= match.start() and match.end() <= end for start, end in excluded_spans):
continue
value = match.group()
if value in seen or len(candidates) >= MAX_HEURISTIC_RESERVATION_CANDIDATES:
continue
seen.add(value)
evidence = {**base_evidence, 'raw_value': value}
if 'cell_id' not in evidence:
evidence['cell_id'] = f"reservation-heuristic:{content_hash}:line{evidence.get('row_index')}"
evidence['context'] = _heuristic_context_snippet(text, match)
candidates.append({'value': value, 'evidence': evidence})
for cell_text, base_evidence in _iter_heuristic_cell_texts(html_tables, content_hash):
_collect(cell_text, dict(base_evidence))
for line_index, line in enumerate(str(body_text or '').splitlines()):
if line.strip():
_collect(line, {'table_index': None, 'row_index': line_index + 1, 'column_index': None})
if not _heuristic_cell_is_bare_number(cell_text):
continue
value = str(cell_text).strip()
if value in seen or len(candidates) >= MAX_HEURISTIC_RESERVATION_CANDIDATES:
continue
seen.add(value)
candidates.append({'value': value, 'evidence': {**base_evidence, 'raw_value': value}})
return candidates