From f77f2670919c03af77f8c21f47a64c70eada3cbd Mon Sep 17 00:00:00 2001 From: caohuipeng Date: Tue, 15 Sep 2026 15:22:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(shipping-notice):=20=E6=94=B6=E7=B4=A7?= =?UTF-8?q?=E9=82=AE=E4=BB=B6=E8=A1=A5=E5=85=85=E5=8C=B9=E9=85=8D=E5=B9=B6?= =?UTF-8?q?=E5=BD=92=E4=B8=80=E9=A2=84=E7=BA=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../email_allocation_table_parser.py | 9 ++ .../shipping_notice/order_supplement_merge.py | 65 +++++++-- .../shipping_notice/reservation_id.py | 26 +++- backend/tests/test_email_supplement_merge.py | 134 ++++++++++++++++-- .../test_shipping_notice_reservation_id.py | 12 ++ docs/features/shipping-notice/technical.md | 6 +- ...5-email-supplement-po-absent-mpn-design.md | 57 ++++++++ frontend/src/locales/en/chat.json | 1 + frontend/src/locales/th/chat.json | 1 + frontend/src/locales/zh-CN/chat.json | 1 + frontend/src/locales/zh-TW/chat.json | 1 + 11 files changed, 282 insertions(+), 31 deletions(-) create mode 100644 docs/superpowers/specs/2026-09-15-email-supplement-po-absent-mpn-design.md diff --git a/backend/app/services/shipping_notice/email_allocation_table_parser.py b/backend/app/services/shipping_notice/email_allocation_table_parser.py index e7bb63b7..7acadc8d 100644 --- a/backend/app/services/shipping_notice/email_allocation_table_parser.py +++ b/backend/app/services/shipping_notice/email_allocation_table_parser.py @@ -4,6 +4,7 @@ 逻辑:订单字段表头只复用 Run/当前发布 Schema 的显式别名;邮件专属的分配事实 仍使用受控固定列。表格歧义或缺 PO 保留原因,不将正文直接转成订单。 非法非 PO 字段跳过本列并记 issue,避免可选列拖垮 Reservation/库存源线索。 +reservation_id 通过安全抽取后写入归一本体(含短数字行序后缀)。 多表按文档顺序叠行:靠前为新,同行身份键先到为准;旧表仅补全新表未覆盖的行。 """ @@ -22,6 +23,7 @@ from app.services.shipping_notice.order_schema.field_rules import ( ) from app.services.shipping_notice.order_schema.table_contract import normalize_table_header from app.services.shipping_notice.po_number import normalize_po_number +from app.services.shipping_notice.reservation_id import validated_reservation_id def _schema_configs(schema_definition: dict[str, Any]) -> dict[str, dict[str, Any]]: @@ -89,6 +91,13 @@ def _normalize_schema_row( if value is None or not validate_order_field_value(value, config): issues.append('email_supplement_field_invalid') continue + # [STEP] reservation_id:校验已通过安全抽取后,落库写入归一本体(如 35354-1 → 35354)。 + if field_code == 'reservation_id': + cleaned = validated_reservation_id(raw_value) or validated_reservation_id(value) + if not cleaned: + issues.append('email_supplement_field_invalid') + continue + value = cleaned normalized[field_code] = value po_code = 'customer_po_number' if 'customer_po_number' in normalized else 'po_number' po_number = normalize_po_number(normalized.get(po_code)) diff --git a/backend/app/services/shipping_notice/order_supplement_merge.py b/backend/app/services/shipping_notice/order_supplement_merge.py index d463128a..fd75403b 100644 --- a/backend/app/services/shipping_notice/order_supplement_merge.py +++ b/backend/app/services/shipping_notice/order_supplement_merge.py @@ -2,7 +2,8 @@ 依赖:Run 冻结 Schema、正文补充表契约、PO 标准化、Reservation ID 校验。 逻辑:优先 PO+MPN;其次同 PO+合法 reservation_id 对齐;仅唯一签名的 PO 可降级广播; - 歧义只写复核原因。reservation 对齐与「PO+料号灌预约号」同趟单次合并,不互相重入。 + 无同 PO 候选时仅对「PO 缺失」行做唯一料号兜底(mpn_fallback);有 PO 且与 PDF + 不一致则禁止补充。歧义只写复核原因。同趟单次合并,不互相重入。 """ from __future__ import annotations @@ -277,6 +278,7 @@ def merge_email_supplements(*, orders: list[dict[str, Any]], supplements_by_inta 按冻结 Schema 逐字段合并正文事实,并对歧义订单附加复核冲突。 核心逻辑:同趟单次合并,优先级为 po_mpn → po_reservation → po_fallback → mpn_fallback; + mpn_fallback 仅扫描 PO 缺失行上的唯一料号命中,禁止「错 PO + 料号」擅自补充; 不因写入 reservation 再重入。po_reservation 按行对齐合法预约号,用于灌入库存源线索等。 """ configs = { @@ -327,19 +329,37 @@ def merge_email_supplements(*, orders: list[dict[str, Any]], supplements_by_inta selected = candidates relation = 'po_fallback' if not selected and not candidates: - # 邮件正文的 PO 可能被上游人工录入错误;仅唯一精确 MPN 才能安全补证。 - mpn_matches = [ + # [STEP] 无同 PO 候选:仅 PO 缺失行可走料号兜底;有 PO 但与 PDF 不一致则禁止补充。 + # 按与订单相交的料号分组:每组签名唯一才纳入;任一组多预约/多签名则整单不走本档。 + absent_po_rows = [ row for row in rows - if _schema_field_values(row, part_codes) & order_part_values + if isinstance(row, dict) + and not normalize_atomic_po_number(row.get('customer_po_number') or row.get('po_number')) ] - distinct_matches = { - tuple(str(row.get(field) or '').strip() for field in ( - *configs.keys(), - )): row - for row in mpn_matches - } - if len(distinct_matches) == 1: - selected = list(distinct_matches.values()) + part_groups: dict[frozenset[str], list[dict[str, Any]]] = {} + for row in absent_po_rows: + hit_parts = frozenset(_schema_field_values(row, part_codes) & order_part_values) + if not hit_parts: + continue + part_groups.setdefault(hit_parts, []).append(row) + selected_rows: list[dict[str, Any]] = [] + part_group_ambiguous = False + for group_rows in part_groups.values(): + distinct = { + tuple(str(row.get(field) or '').strip() for field in configs.keys()): row + for row in group_rows + } + if len(distinct) == 1: + selected_rows.extend(distinct.values()) + else: + part_group_ambiguous = True + break + if part_group_ambiguous: + order.setdefault('conflicts', []).append({ + 'severity': 'blocking', 'reason': 'supplement_relation_ambiguous', + }) + elif selected_rows: + selected = selected_rows relation = 'mpn_fallback' if not selected: if candidates: @@ -382,7 +402,21 @@ def merge_email_supplements(*, orders: list[dict[str, Any]], supplements_by_inta if not isinstance(fields, list): continue value_by_canonical: dict[Any, tuple[str, dict[str, Any]]] = {} - comparable_rows = selected if line_index < 0 or relation == 'po_fallback' else [source_row] + # 头字段与 po_fallback:比较全部 selected。 + # po_mpn / mpn_fallback 行字段:比较所有命中本行料号的 selected,避免同料号多预约先写后冲。 + if line_index < 0 or relation == 'po_fallback': + comparable_rows = selected + elif relation in {'po_mpn', 'mpn_fallback'} and part_codes: + line_part_values = ( + _values(fields, 'part_number', {v: k for k, v in ids.items()}) + | _values(fields, 'customer_part_number', {v: k for k, v in ids.items()}) + ) + comparable_rows = [ + row for row in selected + if _schema_field_values(row, part_codes) & line_part_values + ] or [source_row] + else: + comparable_rows = [source_row] for item in comparable_rows: raw_value = str(item.get(target_code) or '').strip() if raw_value: @@ -408,6 +442,11 @@ def merge_email_supplements(*, orders: list[dict[str, Any]], supplements_by_inta field_id = ids.get(target_code) if not value or not field_id: continue + # 短数字后缀等:能抽出合法本体则写入本体;抽不出则保留原值走冲突/下游门禁。 + if target_code == 'reservation_id': + normalized_reservation = validated_reservation_id(value) + if normalized_reservation: + value = normalized_reservation same_code_values = _conflict_canonical_values( fields, target_code, {v: k for k, v in ids.items()}, config, ) diff --git a/backend/app/services/shipping_notice/reservation_id.py b/backend/app/services/shipping_notice/reservation_id.py index 8aea5a1a..5a837afb 100644 --- a/backend/app/services/shipping_notice/reservation_id.py +++ b/backend/app/services/shipping_notice/reservation_id.py @@ -3,7 +3,8 @@ services/shipping_notice/reservation_id.py 用途:提取、标准化并校验发货通知与库存中的 Reservation ID。 依赖:正则表达式与标准 `typing` 工具。 -逻辑:由 notice_lines、inventory_snapshot、match、extract 等多个解析/匹配模块复用本文件完成 Reservation ID 归一化。 +逻辑:由 notice_lines、inventory_snapshot、match、extract、EML 补充解析等多个模块复用; + 归一结果恒为 5/10 位纯数字(可剥括号备注、@ 前缀、短数字行序后缀如 35354-1)。 """ from __future__ import annotations @@ -23,6 +24,8 @@ INTERNAL_UUID_RE = re.compile( RESERVATION_VALUE_RE = re.compile(r"^(\d{10}|\d{5})(?!\d)\s*(?:\([^()\r\n]*\)|([^()\r\n]*))?\s*$") # ``@`` 是预约号与前缀说明的显式分隔,后缀复用 Reservation 值的括号备注契约。 RESERVATION_AT_SUFFIX_RE = re.compile(r"@\s*(\d{10}|\d{5})(?!\d)(?=\s*(?:\([^()\r\n]*\)|([^()\r\n]*))?\s*$)") +# QUANTA IR# 等:合法 5/10 位本体 + 一段短数字行序后缀(- / - / _);多段后缀拒绝。 +RESERVATION_SHORT_NUMERIC_SUFFIX_RE = re.compile(r"^(\d{10}|\d{5})[--_](\d+)$") PR_NUMBER_LINE_RE = re.compile( r"PR\s*Number\s*[::]?\s*([^\n\r]+)", re.IGNORECASE, @@ -59,25 +62,38 @@ def normalize_reservation_id(value: Any) -> str: def extract_reservation_id(value: Any) -> str: - """标准化 Reservation ID,并识别 ``@`` 后带可选括号备注的预约号。 + """标准化 Reservation ID,并识别括号备注、``@`` 分隔与短数字行序后缀。 + + 核心逻辑:先匹配纯 5/10 位(可带括号备注);再搜 ``@`` 后本体;最后剥离 + 一段 ``-``/``-``/``_`` + 数字后缀(如 ``35354-1`` → ``35354``)。多段后缀不采纳。 Args: - value: Reservation ID 字段原始值;仅允许整体纯数字,或明确 ``@`` 分隔后可带括号备注的纯数字。 + value: Reservation ID 字段原始值。 Returns: - str: 已确认的五位或十位纯数字 Reservation ID;普通尾随文本或金额文本返回空字符串。 + str: 已确认的五位或十位纯数字 Reservation ID;无法安全抽取时返回空字符串。 """ text = str(value or "").strip() if not text: return "" + # [STEP 1] 整体纯数字(可选括号备注) direct = normalize_reservation_id(text) direct_match = RESERVATION_VALUE_RE.fullmatch(direct) if direct_match: return direct_match.group(1) + # [STEP 2] 显式 @ 分隔(可带括号备注) match = RESERVATION_AT_SUFFIX_RE.search(text) - return match.group(1) if match else "" + if match: + return match.group(1) + + # [STEP 3] 短数字行序后缀(IR# 35354-1);仅一段后缀 + suffix_match = RESERVATION_SHORT_NUMERIC_SUFFIX_RE.fullmatch(direct) + if suffix_match: + return suffix_match.group(1) + + return "" def find_reservation_id_in_document(text: str) -> str: diff --git a/backend/tests/test_email_supplement_merge.py b/backend/tests/test_email_supplement_merge.py index 44cee41f..e609fb71 100644 --- a/backend/tests/test_email_supplement_merge.py +++ b/backend/tests/test_email_supplement_merge.py @@ -2,7 +2,7 @@ 依赖:正文补充解析器与合并服务。 逻辑:正文只补 Reservation/来源字段,PDF 数量始终保持权威;关联优先 PO+料号, - 其次 PO+预约号,歧义稳定进入复核。 + 其次 PO+预约号;无同 PO 时仅 PO 缺失行可料号兜底;错 PO 禁止补充。 """ from __future__ import annotations @@ -23,7 +23,7 @@ SCHEMA = {'fields': [ {'field_id': 'qty', 'field_code': 'qty'}, { 'field_id': 'reservation', 'field_code': 'reservation_id', 'scope': 'line', - 'aliases': ['Reservation ID', 'External reservation ID', 'Reservation Number'], + 'aliases': ['Reservation ID', 'External reservation ID', 'Reservation Number', 'IR#'], }, {'field_id': 'allocation', 'field_code': 'allocation_id'}, {'field_id': 'source', 'field_code': 'inventory_source_hint', 'scope': 'line', 'aliases': ['Source Type']}, @@ -213,7 +213,12 @@ def test_email_supplement_pdf_value_conflict_keeps_pdf_and_requires_review(): )[0] assert _value(merged['lines'][0]['fields'], 'reservation') == 'PDF-33873' - assert {'severity': 'blocking', 'reason': 'supplement_field_conflict', 'field': 'reservation_id'} in merged['conflicts'] + assert any( + item.get('severity') == 'blocking' + and item.get('reason') == 'supplement_field_conflict' + and item.get('field') == 'reservation_id' + for item in merged['conflicts'] + ) @pytest.mark.pure_unit @@ -337,12 +342,12 @@ def test_email_supplement_cross_adjudicates_pn_and_ampn_against_both_order_part_ order['lines'][0]['fields'][0]['observations'][0]['value'] = value supplement = {'document_id': 44, 'rows': [{ 'po_number': '4518738152', mail_field: value, - 'reservation_id': f'30000{index}', 'evidence': {}, + 'reservation_id': f'300000000{index}', 'evidence': {}, }]} merged = merge_email_supplements( orders=[order], supplements_by_intake={1: supplement}, schema_definition=SCHEMA, ) - assert _value(merged[0]['lines'][0]['fields'], 'reservation') == f'30000{index}' + assert _value(merged[0]['lines'][0]['fields'], 'reservation') == f'300000000{index}' assert merged[0]['_hxops_email_supplement_relation'] == 'po_mpn' @@ -447,17 +452,13 @@ def test_email_supplement_po_fallback_requires_one_reservation(): assert any(item.get('reason') == 'supplement_relation_ambiguous' for item in result['conflicts']) -def test_email_supplement_unique_mpn_fallback_handles_mistyped_email_po(): - """邮件 PO 与 PDF 不一致时,唯一完全匹配 MPN 仍可保守补齐预约号。""" +def test_email_supplement_unique_mpn_fallback_rejects_mistyped_email_po(): + """邮件 PO 有值且与 PDF 不一致时禁止 mpn_fallback,哪怕料号唯一命中。""" order = _order() order['header_fields'][0]['observations'][0]['value'] = '4518838152' supplement = { 'document_id': 44, 'rows': [ - { - 'customer_po_number': '4518738152', 'part_number': 'D4-A08AR12000002', - 'reservation_id': '2000484577', 'evidence': {}, - }, { 'customer_po_number': '4518738152', 'part_number': 'D4-A08AR12000002', 'reservation_id': '2000484577', 'evidence': {}, @@ -472,10 +473,121 @@ def test_email_supplement_unique_mpn_fallback_handles_mistyped_email_po(): orders=[order], supplements_by_intake={1: supplement}, schema_definition=SCHEMA, )[0] + assert not any(item['field_id'] == 'reservation' for item in result['lines'][0]['fields']) + assert result.get('_hxops_email_supplement_relation') is None + + +def test_email_supplement_mpn_fallback_when_po_absent_and_part_unique(): + """PO 列缺失时,唯一料号命中仍可保守补齐预约号与库存源线索。""" + order = _order() + result = merge_email_supplements( + orders=[order], + supplements_by_intake={1: {'document_id': 44, 'rows': [{ + 'part_number': 'D4-A08AR12000002', + 'reservation_id': '2000484577', + 'inventory_source_hint': 'DBS SIN', + 'evidence': {}, + }]}}, + schema_definition=SCHEMA, + )[0] assert _value(result['lines'][0]['fields'], 'reservation') == '2000484577' + assert _value(result['lines'][0]['fields'], 'source') == 'DBS SIN' assert result['_hxops_email_supplement_relation'] == 'mpn_fallback' +def test_email_supplement_single_line_po_absent_still_requires_part_match(): + """双边各一行且 PO 缺失时仍须料号对上;对不上不得广播预约号。""" + order = _order() + result = merge_email_supplements( + orders=[order], + supplements_by_intake={1: {'document_id': 44, 'rows': [{ + 'part_number': 'OTHER-PART-0001', + 'reservation_id': '2000484577', + 'evidence': {}, + }]}}, + schema_definition=SCHEMA, + )[0] + assert not any(item['field_id'] == 'reservation' for item in result['lines'][0]['fields']) + assert result.get('_hxops_email_supplement_relation') is None + + +def test_email_supplement_po_absent_unique_reservation_per_part_merges_rows(): + """多行 PO 缺失时按料号唯一预约号逐行补充;同料号多预约则歧义。""" + order = { + '_hxops_intake_id': 1, + 'header_fields': [{'field_id': 'po', 'observations': [{'value': '4518738152'}]}], + 'lines': [ + {'fields': [ + {'field_id': 'part', 'observations': [{'value': 'D4-A08AR12000002'}]}, + {'field_id': 'qty', 'observations': [{'value': 1}]}, + ]}, + {'fields': [ + {'field_id': 'part', 'observations': [{'value': 'D4-A08AR12000003'}]}, + {'field_id': 'qty', 'observations': [{'value': 2}]}, + ]}, + ], + 'conflicts': [], + } + merged = merge_email_supplements( + orders=[order], + supplements_by_intake={1: {'document_id': 44, 'rows': [ + {'part_number': 'D4-A08AR12000002', 'reservation_id': '2000484577', 'evidence': {}}, + {'part_number': 'D4-A08AR12000003', 'reservation_id': '2000484578', 'evidence': {}}, + ]}}, + schema_definition=SCHEMA, + )[0] + assert merged['_hxops_email_supplement_relation'] == 'mpn_fallback' + assert _value(merged['lines'][0]['fields'], 'reservation') == '2000484577' + assert _value(merged['lines'][1]['fields'], 'reservation') == '2000484578' + + ambiguous = merge_email_supplements( + orders=[order], + supplements_by_intake={1: {'document_id': 44, 'rows': [ + {'part_number': 'D4-A08AR12000002', 'reservation_id': '2000484577', 'evidence': {}}, + {'part_number': 'D4-A08AR12000002', 'reservation_id': '2000484579', 'evidence': {}}, + {'part_number': 'D4-A08AR12000003', 'reservation_id': '2000484578', 'evidence': {}}, + ]}}, + schema_definition=SCHEMA, + )[0] + assert any(item.get('reason') == 'supplement_relation_ambiguous' for item in ambiguous['conflicts']) + assert not any(item['field_id'] == 'reservation' for item in ambiguous['lines'][0]['fields']) + assert not any(item['field_id'] == 'reservation' for item in ambiguous['lines'][1]['fields']) + + +def test_email_supplement_normalizes_reservation_short_numeric_suffix_on_merge(): + """同 PO 命中后,IR# 形态 35354-1 写入订单行时归一为 35354。""" + result = merge_email_supplements( + orders=[_order()], + supplements_by_intake={1: {'document_id': 44, 'rows': [{ + 'customer_po_number': '4518738152', + 'part_number': 'D4-A08AR12000002', + 'reservation_id': '35354-1', + 'evidence': {}, + }]}}, + schema_definition=SCHEMA, + )[0] + assert _value(result['lines'][0]['fields'], 'reservation') == '35354' + assert result['_hxops_email_supplement_relation'] == 'po_mpn' + + +def test_email_supplement_html_ir_hash_suffix_survives_parse_and_merge(): + """解析 IR#=35354-1 不得因非法形态丢列,合并后预约号为 35354。""" + supplement = parse_email_allocation_tables( + content_hash='i' * 64, + html_tables=[''' +
PO#MPNIR#
4518738152D4-A08AR1200000235354-1
'''], + schema_definition=SCHEMA, + ) + assert supplement['rows'][0]['reservation_id'] == '35354' + assert 'email_supplement_field_invalid' not in supplement.get('issues', []) + merged = merge_email_supplements( + orders=[_order()], + supplements_by_intake={1: {**supplement, 'document_id': 44}}, + schema_definition=SCHEMA, + )[0] + assert _value(merged['lines'][0]['fields'], 'reservation') == '35354' + + def test_email_supplement_plain_text_vertical_table_is_parsed_without_guessing_body_text(): """Outlook 展平的纵向表头可恢复补充行,尾部邮件正文不会被误当数据。""" supplement = parse_email_allocation_text( diff --git a/backend/tests/test_shipping_notice_reservation_id.py b/backend/tests/test_shipping_notice_reservation_id.py index f7bc015d..63671232 100644 --- a/backend/tests/test_shipping_notice_reservation_id.py +++ b/backend/tests/test_shipping_notice_reservation_id.py @@ -71,6 +71,18 @@ def test_validated_reservation_id_strips_optional_parenthesized_note(): assert validated_reservation_id("30001129345") == "" +def test_validated_reservation_id_strips_short_numeric_line_suffix(): + """QUANTA IR# 等:合法 5/10 位本体 + 一段短数字后缀归一为本体;多段/非法左侧拒绝。""" + assert validated_reservation_id("35354-1") == "35354" + assert validated_reservation_id("3000112934-2") == "3000112934" + assert validated_reservation_id("35354-1") == "35354" + assert validated_reservation_id("35354_1") == "35354" + assert validated_reservation_id("1234-1") == "" + assert validated_reservation_id("35354-1-2") == "" + assert validated_reservation_id("35354-a") == "" + assert validated_reservation_id("35354123456-1") == "" + + def test_validated_reservation_id_rejects_amount_text_as_numeric_token(): """金额行中的五位整数片段不能被当成 Reservation ID。""" assert validated_reservation_id("941.76 28,252.80") == "" diff --git a/docs/features/shipping-notice/technical.md b/docs/features/shipping-notice/technical.md index 028b2a2a..7e517976 100644 --- a/docs/features/shipping-notice/technical.md +++ b/docs/features/shipping-notice/technical.md @@ -399,7 +399,7 @@ L1 字段级:目标单元格 + 表头 + 相邻单元格 **版面签名证据一致性(2026-08-04)**:表头签名不再扫描整页顶部或按表高固定比例猜测,而是直接冻结模板既有 `header_layout.region`;运行时对每个候选在同一区域独立取证。新旧 PDF 坐标证据统一按视觉行和纯几何词间距恢复表头词组,避免 `boundary_before` 有无造成同文件签名漂移;列中心和列宽由表头词组边界产生,不再使用全表单字符连续聚类。只有同时达到模板最低分和证据覆盖率的候选才参与唯一性与 Margin 决胜。 -**邮件补充重建规则(2026-07-31)**:邮件表头只能按当前 Run 冻结 Schema 的显式别名映射;行级 `reservation_id` 包含真实业务表头 `External reservation ID`。只要 Intake 保留原始 `body_html_tables`,Run 冻结时必须按自己的 Schema 重新解析完整补充表,不能复用上传阶段缺字段但已有部分行的旧结果;没有 HTML 时才复用已有完整结果或安全回退纵向纯文本解析。 +**邮件补充重建规则(2026-07-31)**:邮件表头只能按当前 Run 冻结 Schema 的显式别名映射;行级 `reservation_id` 包含真实业务表头 `External reservation ID`。只要 Intake 保留原始 `body_html_tables`,Run 冻结时必须按自己的 Schema 重新解析完整补充表,不能复用上传阶段缺字段但已有部分行的旧结果;没有 HTML 时才复用已有完整结果或安全回退纵向纯文本解析。**关联收紧(2026-09-15)**:`mpn_fallback` 仅适用于补充行 PO 缺失且料号唯一命中;有 PO 且与 PDF 不一致不得补充。预约号允许短数字行序后缀归一(如 `35354-1` → `35354`)。 **`llm_extraction` 回退语义(2026-08-01)**:订单字段标记为 `llm_extraction` 表示“确定性解析无法取值时允许 LLM 补充”,不表示必须使用 LLM。通用解析门禁应依据字段是否已有完整、无冲突的确定性证据裁决;已取得必填 Header/Line 值时可直通,缺失时由 `required_field_missing` 转入 LLM/人工复核。 @@ -3334,7 +3334,9 @@ API 层 邮件正文保持 `email_context`,但其受控 HTML 表会被确定性解析为 PO 级补充事实,而不是订单 Candidate 或订单抽取 Unit。每个补充值冻结 EML Intake、正文 Document、表格/行/列、DOM `cell_id`、原始值与规范化值;Run 仅冻结这项事实,Worker 不会对正文单独执行订单解析。 -合并优先使用 `PO + MPN`;其次同 PO 且双方均有合法 `reservation_id` 时以 `po_reservation` 按行对齐(解决 allocation 表 APN 对不上 PDF 料号时仍可补库存源线索);只有同一 PO 的正文行唯一或唯一对应一个 Reservation 时,才能以 `po_fallback` 补齐。正文可覆盖/补齐 Reservation、Allocation 与库存来源提示,但绝不覆盖 PDF 的 PO、料号或数量;`allocated_qty` 仅作审计参考。多 Reservation、多来源、行无法唯一关联和无效表头均添加 `supplement_relation_ambiguous` 或稳定解析原因,并转入人工复核。 +合并优先使用 `PO + MPN`;其次同 PO 且双方均有合法 `reservation_id` 时以 `po_reservation` 按行对齐(解决 allocation 表 APN 对不上 PDF 料号时仍可补库存源线索);只有同一 PO 的正文行唯一或唯一对应一个 Reservation 时,才能以 `po_fallback` 补齐。无同 PO 候选时,仅对 **PO 缺失** 行按料号唯一命中走 `mpn_fallback`(可多料号逐行);邮件行上有 PO 但与 PDF 不一致时**禁止**补充。正文可覆盖/补齐 Reservation、Allocation 与库存来源提示,但绝不覆盖 PDF 的 PO、料号或数量;`allocated_qty` 仅作审计参考。多 Reservation、多来源、行无法唯一关联和无效表头均添加 `supplement_relation_ambiguous` 或稳定解析原因,并转入人工复核。 + +**预约号短数字后缀(2026-09-15)**:统一抽取允许 `35354-1` / `3000112934-2` 等「合法 5/10 位本体 + 一段 `-`/`-`/`_` 数字后缀」,归一为本体后再参与 EML 校验与合并;多段后缀仍拒绝。详见 `docs/superpowers/specs/2026-09-15-email-supplement-po-absent-mpn-design.md`。 #### 阶段 3:PDF 模板解析、PO 拆分与严格拒识(2026-07-27,迁移前历史) diff --git a/docs/superpowers/specs/2026-09-15-email-supplement-po-absent-mpn-design.md b/docs/superpowers/specs/2026-09-15-email-supplement-po-absent-mpn-design.md new file mode 100644 index 00000000..dc1cb7a4 --- /dev/null +++ b/docs/superpowers/specs/2026-09-15-email-supplement-po-absent-mpn-design.md @@ -0,0 +1,57 @@ +# EML 补充:PO 缺失料号兜底 + 预约号后缀规范化 + +**日期**:2026-09-15 +**状态**:已定稿,待实现/实现中 + +## 1. 背景 + +两例不同断点一并覆盖: + +1. **PO 缺失 / 错 PO**:补充表已解析出预约号或库存源线索,但 `merge_email_supplements` 未写入 PDF 行。旧 `mpn_fallback` 在「邮件 PO 打错但料号唯一」时仍会补,与业务裁决冲突。 +2. **QUANTA 有 PO 仍缺预约**:`PO#` 与 PDF 已对齐,但 `IR#=35354-1` 不满足旧契约「仅 5/10 位纯数字」,EML 解析校验跳过该列,有效预约号落空。 + +## 2. 冻结规则 + +### 2.1 邮件关联(`order_supplement_merge`) + +优先级保持:`po_mpn` → `po_reservation` → `po_fallback` → `mpn_fallback`。 + +| 规则 | 行为 | +|------|------| +| EML 行 PO 有值且规范化后 ∉ PDF PO 集合 | **禁止**补充(即使料号唯一) | +| EML 行 PO 缺失(无列或空) | 允许按料号兜底(`mpn_fallback` 仅扫 absent_po_rows) | +| 单行 EML + 单行订单 | 仍须料号命中;只去掉 PO 依赖 | +| 同料号唯一预约 + PO 缺失 | 按行补 | +| 同料号多个不同预约 | `supplement_relation_ambiguous` | +| 料号对不上 | 不补 | +| 库存源线索 | 只认冻结 Schema 别名映射字段 | +| 正文自由文本 PO | 不做 | + +### 2.2 预约号短数字后缀(`reservation_id.extract_reservation_id`) + +| 规则 | 行为 | +|------|------| +| `^(5或10位)[--_](\d+)$` | 取左侧本体(例:`35354-1` → `35354`) | +| 括号备注 / `@` 分隔 | 先试既有规则,再试短数字后缀 | +| 左侧非 5/10 位、多段后缀、字母后缀 | 拒绝 | + +归一结果仍为 5/10 位纯数字;Schema `normalizer` / EML 校验 / `validated_reservation_id` 共用此抽取。 + +## 3. 非目标 + +- 自由文本抽 PO、非 Schema 别名库存源、无视料号单行广播 +- 配置化关联决策表、错 PO 专用 Finding、字段冲突 UI 拆分 + +## 4. 落点 + +- `backend/app/services/shipping_notice/reservation_id.py` +- `backend/app/services/shipping_notice/order_supplement_merge.py` +- `backend/tests/test_shipping_notice_reservation_id.py` +- `backend/tests/test_email_supplement_merge.py` +- `frontend/src/locales/*/chat.json`(`mpn_fallback`) +- `docs/features/shipping-notice/technical.md` + +## 5. 验收 + +- 错 PO 不补;PO 缺失 + 唯一料号可补;`35354-1` → 有效 `35354` +- `mpn_fallback` 四语言文案可见 diff --git a/frontend/src/locales/en/chat.json b/frontend/src/locales/en/chat.json index 9c4cdfbe..3303ec0c 100644 --- a/frontend/src/locales/en/chat.json +++ b/frontend/src/locales/en/chat.json @@ -438,6 +438,7 @@ "po_mpn": "Relation: PO + part number", "po_reservation": "Relation: PO + reservation ID", "po_fallback": "Relation: matched by PO", + "mpn_fallback": "Relation: missing PO + part number", "unknown": "Relation pending confirmation" }, "shippingOrderExtractionEmailSupplementField": { diff --git a/frontend/src/locales/th/chat.json b/frontend/src/locales/th/chat.json index a3bc4fa1..335e56d0 100644 --- a/frontend/src/locales/th/chat.json +++ b/frontend/src/locales/th/chat.json @@ -438,6 +438,7 @@ "po_mpn": "วิธีเชื่อมโยง: PO + หมายเลขชิ้นส่วน", "po_reservation": "วิธีเชื่อมโยง: PO + รหัสการจอง", "po_fallback": "วิธีเชื่อมโยง: จับคู่ด้วย PO", + "mpn_fallback": "วิธีเชื่อมโยง: ไม่มี PO + หมายเลขชิ้นส่วน", "unknown": "รอยืนยันวิธีเชื่อมโยง" }, "shippingOrderExtractionEmailSupplementField": { diff --git a/frontend/src/locales/zh-CN/chat.json b/frontend/src/locales/zh-CN/chat.json index c010eacf..5f992929 100644 --- a/frontend/src/locales/zh-CN/chat.json +++ b/frontend/src/locales/zh-CN/chat.json @@ -472,6 +472,7 @@ "po_mpn": "关联方式:PO + 料号", "po_reservation": "关联方式:PO + 预约号", "po_fallback": "关联方式:按 PO 匹配", + "mpn_fallback": "关联方式:PO 缺失 + 料号", "unknown": "关联方式待确认" }, "shippingOrderExtractionEmailSupplementField": { diff --git a/frontend/src/locales/zh-TW/chat.json b/frontend/src/locales/zh-TW/chat.json index 1f322c77..96bdf055 100644 --- a/frontend/src/locales/zh-TW/chat.json +++ b/frontend/src/locales/zh-TW/chat.json @@ -438,6 +438,7 @@ "po_mpn": "關聯方式:PO + 料號", "po_reservation": "關聯方式:PO + 預約號", "po_fallback": "關聯方式:依 PO 配對", + "mpn_fallback": "關聯方式:PO 缺失 + 料號", "unknown": "關聯方式待確認" }, "shippingOrderExtractionEmailSupplementField": {