feat(shipping-notice): 落地字段资产治理与唯一 Schema,自动建单随版本冻结

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-07-29 16:23:15 +08:00
1 parent b7307fc573
commit 3a1dbf7ce5
144 files changed
+15167 -4480

No files matched your search

@@ -60,28 +60,33 @@ def _seed_candidate(*, sender: str = ''):
def _published_field(*, code: str, option_key: str | None = None):
"""发布允许客户识别的 Header 字段,并返回字段、版本和冻结配置。"""
from app.services.shipping_notice import field_asset_service
del option_key # mail/context 字段不再登记为订单资产;测试改用语义等价的 Header 字段值。
schema = ShippingOrderSchema.query.filter_by(schema_code='SHIPPING_ORDER').one_or_none()
if schema is None:
schema = ShippingOrderSchema(schema_code='SHIPPING_ORDER', display_name='订单')
db.session.add(schema)
db.session.flush()
field = ShippingOrderField(
schema_id=schema.id, field_code=code, scope='header', value_type='string',
field_category='general_context' if option_key else 'order_header',
collection_mode='deterministic_context' if option_key else 'llm_extraction',
)
db.session.add(field)
db.session.flush()
field_asset_service.ensure_standard_assets_seeded()
field = ShippingOrderField.query.filter_by(field_code=code).one_or_none()
if field is None:
field = ShippingOrderField(
field_code=code, scope='header', value_type='string',
field_category='order_header', collection_mode='llm_extraction',
asset_kind='extension', storage_mode='json_only', status='active',
display_name_default=code,
)
db.session.add(field)
db.session.flush()
current = ShippingOrderSchemaVersion.query.filter_by(status='published').one_or_none()
if current is not None:
current.status = 'retired'
config = {
'field_id': field.id, 'field_code': code, 'display_name': code, 'scope': 'header',
'value_type': 'string', 'enabled': True, 'available_for_customer_identity': True,
'normalizer_key': 'lowercase_trim' if option_key == 'from_email' else 'uppercase_trim',
'normalizer_key': 'lowercase_trim',
}
if option_key:
config['context_source'] = {'provider_key': 'mail_header', 'option_key': option_key}
version = ShippingOrderSchemaVersion(
schema_id=schema.id, version=ShippingOrderSchemaVersion.query.count() + 1,
status='published', definition_json={'fields': [config]}, checksum=f'{ShippingOrderSchemaVersion.query.count() + 1:064x}',
@@ -138,7 +143,7 @@ def _customer(name, code):
def test_accepted_field_value_resolves_customer_without_mail_header_bypass(app):
"""客户命中必须来自 accepted FieldValue;Intake 邮件头即使不同也不能覆盖。"""
with app.app_context():
field, version = _published_field(code='sender_email', option_key='from_email')
field, version = _published_field(code='bill_to_customer')
definition, rule_set = _definition(field)
customer = _customer('无锡宇辰有限公司', 'WYC')
identity_rules.create_evidence(
@@ -216,7 +221,7 @@ def test_site_field_resolves_site_only_and_freezes_context_field_snapshot(app):
def test_equal_customer_matches_remain_ambiguous(app):
"""同一已裁决字段值映射多个客户时必须保留歧义。"""
with app.app_context():
field, version = _published_field(code='sender_email', option_key='from_email')
field, version = _published_field(code='bill_to_customer')
definition, _rule_set = _definition(field)
for customer in (_customer('客户甲', 'AAA'), _customer('客户乙', 'BBB')):
identity_rules.create_evidence(
@@ -236,7 +241,7 @@ def test_equal_customer_matches_remain_ambiguous(app):
def test_shadow_resolution_is_idempotent_and_accuracy_uses_labels_only(app):
"""重算复用同一记录;准确率只统计人工标签且不反向绑定业务。"""
with app.app_context():
field, version = _published_field(code='sender_email', option_key='from_email')
field, version = _published_field(code='bill_to_customer')
definition, _rule_set = _definition(field)
customer = _customer('ACME', 'ACM')
identity_rules.create_evidence(