Ver código fonte

master_agent_20261030

jackson 5 dias atrás
pai
commit
b4a3320387

+ 57 - 7
tests/test_goods_ai_pre_audit_tools.py

@@ -8,7 +8,10 @@ from mcp_protocol import McpProtocolHandler
 from public_gateway import PublicGatewayApp
 from public_server import PublicMcpHttpHandler
 from services.output_presenter import OutputPresenter
-from tools.goods_ai_pre_audit_common import HINT_FIELDS, HINT_ROW_FIELDS
+from tools.goods_ai_pre_audit_common import (
+    CERTIFICATION_FILE_GROUPS, HINT_FIELDS, HINT_ROW_FIELDS,
+    SAVE_INPUT_SCHEMA, matches_schema,
+)
 
 
 TOOL_ROUTES = {
@@ -66,6 +69,14 @@ def page_values():
         'yf_clearance_data': [],
         'sync_orders': [{'order_id': '8', 'order_number': 'SO-8', 'status_name': ''}],
     })
+    values.update({
+        'brand_type': '自主品牌', 'brand': 'Brand A', 'type': 'Model B',
+        'clearance_hscode': '1234(税率: 5%, 材质占比: 100%)',
+        'ys_kit_hscode': '1234: 5678', 'ys_adjust_rate': '1234:(加征1: 9901)',
+        'purchase_price': '19.05', 'purchase_price_usd': '3.00(汇率:6.35)',
+        'clearance_price': '2.5000 CAD (13.15 CNY)',
+    })
+    values.update(certification_files())
     return values
 
 
@@ -256,6 +267,30 @@ class GoodsAiPreAuditToolsTest(unittest.TestCase):
         self.assertFalse(result['is_error'])
         self.assertEqual(data, result['structured_content'])
 
+    def test_read_presenter_accepts_current_customer_detail_and_certification_page_fields(self):
+        data = read_data()
+        detail = {
+            'brand_type': '自主品牌', 'brand': 'Brand A', 'type': 'Model B',
+            'clearance_hscode': '1234(税率: 5%, 材质占比: 100%)',
+            'ys_kit_hscode': '1234: 5678', 'ys_adjust_rate': '1234:(加征1: 9901)',
+            'purchase_price': '19.05', 'purchase_price_usd': '3.00(汇率:6.35)',
+            'clearance_price': '2.5000 CAD (13.15 CNY)',
+        }
+        data['page_values'].update(detail)
+        data['page_values'].update(certification_files())
+        result = OutputPresenter().present('query_goods_ai_pre_audit', envelope(data))
+        self.assertFalse(result['is_error'], result['text'])
+        self.assertEqual(data, result['structured_content'])
+        self.assertEqual(data, json.loads(result['text']))
+        save = save_arguments()
+        save['fields'].update({key: leaf(value) for key, value in detail.items()})
+        save['fields'].update({key: leaf(value) for key, value in certification_files().items()})
+        self.assertTrue(matches_schema(save, SAVE_INPUT_SCHEMA),
+                        'read-only customer evidence fields must remain in the exact field roundtrip')
+        save['fields']['battery_file']['suggested_value'] = certification_files()['battery_file']
+        self.assertFalse(matches_schema(save, SAVE_INPUT_SCHEMA),
+                         'certification evidence fields never accept an AI replacement value')
+
     def test_read_presenter_rejects_unknown_and_malformed_nested_content(self):
         mutations = [
             lambda d: d.update(internal_token='private'),
@@ -303,6 +338,8 @@ class GoodsAiPreAuditToolsTest(unittest.TestCase):
         self.assertEqual(original, result['structured_content'])
         self.assertEqual(original, json.loads(result['text']))
         self.assertEqual(original, data)
+        for group, files in original['context']['certification_files'].items():
+            self.assertEqual(files, original['page_values'][group])
         self.assertEqual({'request_id': 'rq_pre_audit'}, result['meta'])
 
     def test_legacy_read_without_certification_files_does_not_invent_empty_groups(self):
@@ -356,6 +393,12 @@ class GoodsAiPreAuditToolsTest(unittest.TestCase):
                     self.assertTrue(result['is_error'])
                     self.assertNotIn('private-certification', json.dumps(result))
 
+                data = read_data()
+                data['page_values'][group] = [valid, item]
+                result = OutputPresenter().present('query_goods_ai_pre_audit', envelope(data))
+                self.assertTrue(result['is_error'])
+                self.assertNotIn('private-certification', json.dumps(result))
+
     def test_read_certification_file_urls_reject_unsafe_or_malformed_values(self):
         urls = (
             '', ' ', 'javascript:alert(1)', 'data:application/pdf;base64,AA==',
@@ -378,8 +421,13 @@ class GoodsAiPreAuditToolsTest(unittest.TestCase):
                 self.assertTrue(OutputPresenter().present(
                     'query_goods_ai_pre_audit', envelope(data),
                 )['is_error'])
+                data = read_data()
+                data['page_values']['other_file'] = [{'name': 'cert.pdf', 'url': url}]
+                self.assertTrue(OutputPresenter().present(
+                    'query_goods_ai_pre_audit', envelope(data),
+                )['is_error'])
 
-    def test_certification_files_are_rejected_outside_readonly_context(self):
+    def test_certification_files_are_limited_to_context_and_page_source_fields(self):
         for key in ('certification_files', *certification_files()):
             value = certification_files() if key == 'certification_files' else certification_files()[key]
             for location in ('top', 'page_values', 'selected_labels', 'context'):
@@ -391,9 +439,11 @@ class GoodsAiPreAuditToolsTest(unittest.TestCase):
                               'selected_labels': data['context']['selected_labels'],
                               'context': data['context']}[location]
                     target[key] = value
-                    self.assertTrue(OutputPresenter().present(
-                        'query_goods_ai_pre_audit', envelope(data),
-                    )['is_error'])
+                    result = OutputPresenter().present('query_goods_ai_pre_audit', envelope(data))
+                    if location == 'page_values' and key in CERTIFICATION_FILE_GROUPS:
+                        self.assertFalse(result['is_error'])
+                    else:
+                        self.assertTrue(result['is_error'])
         for name, data in (
             ('claim_goods_ai_pre_audit', claimed()),
             ('save_goods_ai_pre_audit_result', {'goods_id': 17, 'saved': True,
@@ -405,7 +455,7 @@ class GoodsAiPreAuditToolsTest(unittest.TestCase):
                         name, envelope(dict(data, **{key: certification_files()})),
                     )['is_error'])
 
-    def test_local_and_public_inputs_reject_certification_files_and_group_keys(self):
+    def test_local_and_public_inputs_reject_certification_keys_outside_field_rows(self):
         local_client, public_client = LocalClient(), PublicClient()
         local = GatewayApp(api_client=local_client)
         public = PublicGatewayApp(SessionStore(), public_client)
@@ -418,7 +468,7 @@ class GoodsAiPreAuditToolsTest(unittest.TestCase):
             for key in ('context', 'certification_files', *certification_files()):
                 value = certification_files().get(key, certification_files())
                 cases.append((name, dict(payload, **{key: value})))
-                if name == 'save_goods_ai_pre_audit_result':
+                if name == 'save_goods_ai_pre_audit_result' and key in ('context', 'certification_files'):
                     nested = deepcopy(payload)
                     nested['fields'][key] = leaf(value)
                     cases.append((name, nested))

+ 18 - 3
tools/goods_ai_pre_audit_common.py

@@ -24,6 +24,10 @@ PAGE_FIELDS = (
     'ys_clearance_price', 'yf_clearance_price', 'currency',
     'recommended_grade', 'recommended_keyword', 'box_value',
 )
+CUSTOMER_DETAIL_FIELDS = (
+    'brand_type', 'brand', 'type', 'clearance_hscode', 'ys_kit_hscode',
+    'ys_adjust_rate', 'purchase_price', 'purchase_price_usd', 'clearance_price',
+)
 ATTRIBUTE_FIELDS = (
     'is_battery', 'is_magnetic', 'is_wood', 'is_toy', 'is_sensitive', 'is_food',
 )
@@ -85,6 +89,7 @@ for _key in ('is_new_hscode', 'is_new_kit'):
 ROW_VALUES['adjust_hscode_data'] = {'type': 'array', 'items': {'type': 'string'}}
 ROW_VALUES['adjust_rate_data'] = {'type': 'array', 'items': ADJUST_RATE_ROW}
 PAGE_VALUE_SCHEMAS = {key: LEAF_VALUE for key in PAGE_FIELDS}
+PAGE_VALUE_SCHEMAS.update({key: {'type': 'string'} for key in CUSTOMER_DETAIL_FIELDS})
 PAGE_VALUE_SCHEMAS.update({key: {'type': 'boolean'} for key in ATTRIBUTE_FIELDS})
 PAGE_VALUE_SCHEMAS['image_url'] = {
     'anyOf': [{'type': 'null'}, {'type': 'string', 'format': 'uri'}],
@@ -94,7 +99,6 @@ PAGE_VALUE_SCHEMAS.update({
     key: {'type': 'array', 'items': closed_object(ROW_VALUES, CLEARANCE_REQUIRED)}
     for key in CLEARANCE_FIELDS
 })
-PAGE_VALUES_SCHEMA = dict(closed_object(PAGE_VALUE_SCHEMAS), minProperties=1)
 CERTIFICATION_FILE_GROUPS = ('battery_file', 'fda_file', 'toy_file', 'other_file')
 CERTIFICATION_FILE_SCHEMA = closed_object({
     'name': {'type': 'string'},
@@ -104,6 +108,11 @@ CERTIFICATION_FILES_SCHEMA = closed_object({
     group: {'type': 'array', 'items': CERTIFICATION_FILE_SCHEMA}
     for group in CERTIFICATION_FILE_GROUPS
 }, CERTIFICATION_FILE_GROUPS)
+PAGE_VALUE_SCHEMAS.update({
+    group: {'type': 'array', 'items': CERTIFICATION_FILE_SCHEMA}
+    for group in CERTIFICATION_FILE_GROUPS
+})
+PAGE_VALUES_SCHEMA = dict(closed_object(PAGE_VALUE_SCHEMAS), minProperties=1)
 CONTEXT_SCHEMA = closed_object({
     'country_code': {'type': 'string'},
     'departure_country': {'type': 'string'},
@@ -116,11 +125,13 @@ CONTEXT_SCHEMA = closed_object({
 }, ('country_code', 'departure_country', 'selected_labels'))
 
 
-def field_result_schema(value_schema):
+def field_result_schema(value_schema, suggested_value_schema=None):
+    if suggested_value_schema is None:
+        suggested_value_schema = {'anyOf': [{'type': 'null'}, value_schema]}
     return closed_object({
         'value': value_schema,
         'result': {'type': 'string', 'enum': list(FIELD_RESULTS)},
-        'suggested_value': {'anyOf': [{'type': 'null'}, value_schema]},
+        'suggested_value': suggested_value_schema,
         'reason': {'type': 'string'},
         'reason_brief': brief_schema(REASON_BRIEF_MAX),
     }, ('value', 'result', 'suggested_value', 'reason', 'reason_brief'))
@@ -135,6 +146,10 @@ FIELD_SCHEMAS.update({
         field: field_result_schema(value) for field, value in ROW_VALUES.items()
     }, CLEARANCE_REQUIRED)} for key in CLEARANCE_FIELDS
 })
+FIELD_SCHEMAS.update({
+    key: field_result_schema(PAGE_VALUE_SCHEMAS[key], {'type': 'null'})
+    for key in CERTIFICATION_FILE_GROUPS
+})
 CLAIM_INPUT_SCHEMA = closed_object({'goods_id': GOODS_ID})
 QUERY_INPUT_SCHEMA = closed_object({
     'goods_id': GOODS_ID, 'claim_ref': REFERENCE,

+ 2 - 1
tools/query_goods_ai_pre_audit.py

@@ -12,7 +12,8 @@ class QueryGoodsAiPreAuditTool(GoodsAiPreAuditTool):
         '和只读 context;AI 同时读取其中 certification_files 的认证文件。'
         'certification_files 为 battery_file/fda_file/toy_file/other_file 四组 name/url 数组,'
         '空数组表示无文件;历史响应缺少该键表示本次未提供,不能当作无文件。'
-        '认证文件仅作上下文,不加入 page_values 或保存 fields。'
+        '当前页面值同时包含品牌/型号/清关展示值/价格,以及四组同名认证文件来源字段;文件字段与 context.certification_files 同组一致,'
+        '是只读核对项,保存时按原值回传且 suggested_value 必须为 null。'
         '保存时使用 save_goods_ai_pre_audit_result,原样带回 claim_ref 和 source_ref'
         ';逐字段回显 page_values 的原值及完整清关行。禁止使用:未领取就读取、猜测资料版本、执行正式审核或改写身份。'
         '响应机器键、编码、空值和数组顺序必须完整保留,不翻译或省略。'

+ 2 - 1
tools/save_goods_ai_pre_audit_result.py

@@ -18,6 +18,7 @@ class SaveGoodsAiPreAuditResultTool(GoodsAiPreAuditTool):
         '只能判 pass 或 not_checked,从提示看出的问题记到对应审核字段;'
         '其余字段全部 not_checked 时整体 result 只能是 manual。'
         'claim_ref/source_ref 原样带回;认证文件变化后须重新读取当前资料并预审。'
-        'context.certification_files 及四组附件键仅供读取,不得加入保存入参或 fields。'
+        '四组认证文件来源字段(battery_file/fda_file/toy_file/other_file)必须作为 page_values 对应字段加入 fields,'
+        'value 原样回传且 suggested_value=null;不得另加 context 或独立附件键。'
         '禁止使用:省略可见字段、伪造原值、保存过期领取或旧资料、提交正式人工审核、改写业务状态或身份。机器键和值不得翻译或省略。'
     )