diff --git a/backend/app/services/shipping_notice/order_document_profile_validation_service.py b/backend/app/services/shipping_notice/order_document_profile_validation_service.py index b584ee46..1ca8cf97 100644 --- a/backend/app/services/shipping_notice/order_document_profile_validation_service.py +++ b/backend/app/services/shipping_notice/order_document_profile_validation_service.py @@ -1136,6 +1136,20 @@ def _published_representative_samples(profile_id: int) -> list[ShippingOrderDocu ).all() +def published_rival_snapshots_for_overlap(profile: ShippingOrderDocumentProfile) -> list[dict[str, Any]]: + """ + 跨模板唯一性候选:同 Schema 已发布快照,但排除本模板族。 + + 发布时同 profile_code 的旧 published 会被 retire,新草稿不必与自家旧版比唯一性; + 否则 clone 出的 v2 几乎必然与 v1 冲突。 + """ + code = str(profile.profile_code or '') + return [ + item for item in profile_service.published_snapshots_for_schema(profile.schema_version_id) + if str(item.get('profile_code') or '') != code + ] + + def _build_coordinate_overlap_evidence( *, profile: ShippingOrderDocumentProfile, @@ -1152,11 +1166,7 @@ def _build_coordinate_overlap_evidence( definition = dict(profile.definition_json or {}) thresholds = _match_thresholds(definition) schema_definition = profile.schema_version.definition_json if profile.schema_version is not None else {} - published = [ - item for item in profile_service.published_snapshots_for_schema(profile.schema_version_id) - if str(item.get('profile_code') or '') != profile.profile_code - or int(item.get('version') or 0) != int(profile.version) - ] + published = published_rival_snapshots_for_overlap(profile) candidates = [draft_snapshot, *published] sample_role_pollution = _detect_sample_role_pollution(profile) diff --git a/backend/app/services/shipping_notice/order_profile_value_adapters/xfa_dataset.py b/backend/app/services/shipping_notice/order_profile_value_adapters/xfa_dataset.py index a25bf053..f818d8de 100644 --- a/backend/app/services/shipping_notice/order_profile_value_adapters/xfa_dataset.py +++ b/backend/app/services/shipping_notice/order_profile_value_adapters/xfa_dataset.py @@ -1252,7 +1252,6 @@ def build_identity_snapshot(*, profile: Any, revision: Any, samples: list[Any]) def build_overlap_evidence(*, profile: Any, signature_run: Any, training_samples: list[Any], blind_samples: list[Any]) -> tuple[str, dict[str, Any], dict[str, Any]]: """复用共同视觉 overlap,并仅追加 datasets 值源身份冲突证据。""" from app.services.shipping_notice import order_document_profile_validation_service as validation_service - from app.services.shipping_notice import order_document_profile_service as profile_service # [STEP 1] 共同 matcher 负责样本、阈值、正反向命中和污染判定;datasets 不得重建该流程。 status, summary, snapshot = validation_service._build_coordinate_overlap_evidence( @@ -1266,12 +1265,11 @@ def build_overlap_evidence(*, profile: Any, signature_run: Any, training_samples snapshot = dict(snapshot or {}) # [STEP 2] datasets 仅在共同视觉结论后追加正式值源身份的 fail-closed 冲突。 + # 同 profile_code 旧版会在发布时 retire,不与自家模板比身份冲突。 source = (profile.definition_json or {}).get('source_contract') if isinstance((profile.definition_json or {}).get('source_contract'), dict) else {} template, fingerprint = str(source.get('template_packet_checksum') or '').strip(), str(source.get('dataset_structure_fingerprint') or '').strip() conflicts: list[dict[str, Any]] = [] - for item in profile_service.published_snapshots_for_schema(profile.schema_version_id): - if str(item.get('profile_code') or '') == profile.profile_code and int(item.get('version') or 0) == int(profile.version): - continue + for item in validation_service.published_rival_snapshots_for_overlap(profile): other = item.get('definition') if isinstance(item.get('definition'), dict) else {} other_source = other.get('source_contract') if isinstance(other.get('source_contract'), dict) else {} if template and fingerprint and str(other_source.get('template_packet_checksum') or '') == template and str(other_source.get('dataset_structure_fingerprint') or '') == fingerprint: diff --git a/backend/tests/test_order_document_profile_overlap_summary.py b/backend/tests/test_order_document_profile_overlap_summary.py index 0e94d082..788a0ba2 100644 --- a/backend/tests/test_order_document_profile_overlap_summary.py +++ b/backend/tests/test_order_document_profile_overlap_summary.py @@ -7,8 +7,13 @@ tests/test_order_document_profile_overlap_summary.py """ from __future__ import annotations -from app.services.shipping_notice.order_document_profile_validation_service import build_cross_template_overlap_summary -from app.services.shipping_notice.order_document_profile_validation_service import _evaluate_self_sample_match +from types import SimpleNamespace + +from app.services.shipping_notice.order_document_profile_validation_service import ( + build_cross_template_overlap_summary, + published_rival_snapshots_for_overlap, + _evaluate_self_sample_match, +) from app.services.shipping_notice.document_layout.order_profile_matcher import ( ProfileMatchCandidate, ProfileMatchResult, @@ -75,3 +80,24 @@ def test_overlap_ignores_ineligible_candidate_when_unique_match_has_no_eligible_ assert outcome['self_unique'] is True assert outcome['margin'] == 1.0 assert outcome['margin_ok'] is True + + +def test_published_rival_snapshots_exclude_same_profile_code(monkeypatch): + """同模板族旧 published 版不得进入跨模板冲突候选池。""" + from app.services.shipping_notice import order_document_profile_service as profile_service + + monkeypatch.setattr( + profile_service, + 'published_snapshots_for_schema', + lambda _schema_id: [ + {'profile_code': 'ODP-SELF', 'version': 1}, + {'profile_code': 'ODP-SELF', 'version': 2}, + {'profile_code': 'ODP-OTHER', 'version': 1}, + ], + ) + profile = SimpleNamespace(profile_code='ODP-SELF', schema_version_id=1, version=3) + + rivals = published_rival_snapshots_for_overlap(profile) + + assert [item['profile_code'] for item in rivals] == ['ODP-OTHER'] + assert all(item['profile_code'] != 'ODP-SELF' for item in rivals) diff --git a/backend/tests/test_shipping_xfa_formal_extraction_route.py b/backend/tests/test_shipping_xfa_formal_extraction_route.py index f4a6ded0..7770f9c2 100644 --- a/backend/tests/test_shipping_xfa_formal_extraction_route.py +++ b/backend/tests/test_shipping_xfa_formal_extraction_route.py @@ -85,7 +85,6 @@ def test_datasets_blind_cross_check_reports_shared_ocr_mismatch(): def test_datasets_overlap_reuses_common_visual_evidence_then_appends_identity_conflict(monkeypatch): """关键断言:跨模板 overlap 先走共同 matcher,datasets 只能追加末端身份冲突。""" - from app.services.shipping_notice import order_document_profile_service as profile_service from app.services.shipping_notice import order_document_profile_validation_service as validation_service from app.services.shipping_notice.order_profile_value_adapters import xfa_dataset @@ -100,9 +99,9 @@ def test_datasets_overlap_reuses_common_visual_evidence_then_appends_identity_co ), ) monkeypatch.setattr( - profile_service, - 'published_snapshots_for_schema', - lambda _schema_id: [{ + validation_service, + 'published_rival_snapshots_for_overlap', + lambda _profile: [{ 'profile_code': 'XFA-OTHER', 'version': 3, 'checksum': 'other', 'definition': {'source_contract': { 'template_packet_checksum': 'template', @@ -135,6 +134,41 @@ def test_datasets_overlap_reuses_common_visual_evidence_then_appends_identity_co assert snapshot['conflict_profiles'][1]['reason_codes'] == ['value_source_exact_identity_conflict'] +def test_datasets_overlap_skips_same_profile_code_identity_rival(monkeypatch): + """datasets 身份冲突也不得拿同模板族旧版跟自己比。""" + from app.services.shipping_notice import order_document_profile_validation_service as validation_service + from app.services.shipping_notice.order_profile_value_adapters import xfa_dataset + + monkeypatch.setattr( + validation_service, + '_build_coordinate_overlap_evidence', + lambda **_kwargs: ('passed', {'false_hit_other_count': 0}, {'conflict_profiles': []}), + ) + monkeypatch.setattr( + validation_service, + 'published_rival_snapshots_for_overlap', + lambda _profile: [], + ) + profile = SimpleNamespace( + profile_code='XFA-CURRENT', version=2, schema_version_id=9, + definition_json={'source_contract': { + 'template_packet_checksum': 'template', + 'dataset_structure_fingerprint': 'structure', + }}, + ) + + status, summary, snapshot = xfa_dataset.build_overlap_evidence( + profile=profile, + signature_run=SimpleNamespace(id=1), + training_samples=[], + blind_samples=[], + ) + + assert status == 'passed' + assert summary['value_source_identity_conflict_count'] == 0 + assert snapshot['conflict_profiles'] == [] + + def test_datasets_overlap_parse_result_uses_formal_datasets_extraction(monkeypatch): """XFA overlap 解析门禁必须消费 datasets 抽取结果,不能误用坐标字段覆盖。""" from app.services.shipping_notice.order_profile_value_adapters import xfa_dataset