Files
lyun f1a2fbd494 统一:DSH 智能分析、Top100 与图表静默恢复
接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。
补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。
优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。

验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。
仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
2026-11-12 11:57:41 +08:00

2155 lines
163 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Native narration uses mocked providers and verified fixture frames only."""
import copy
import io
import json
import re
import sys
import tempfile
import threading
import unittest
from pathlib import Path
from unittest.mock import patch
from urllib.error import HTTPError
sys.path[:0] = [str(Path(__file__).resolve().parents[1] / 'scripts'), str(Path(__file__).parent)]
from nao_evidence_finalizer import synthesize_native
from nao_native_answer import build_native_report, numeric_inventory, _validate_blocks, _validate_summary_selection, _within_filters, _bind_exact_year_literals, _select_fact_pool, _binding_inventory, _bind_exact_identifiers, _bind_exact_periods, _bind_literal_measures, _metric_semantics, NativeCompositionError, synthesize_native_answer
from nao_native_answer import _affirmative_production_cause, _source_relation_gap, _review_supported_remainder
from test_agent_answer import evidence
def response(value, *, with_summary=True):
# Existing focused fixtures select every supplied short paragraph. Tests
# for missing/invalid selection opt out explicitly; production never infers.
if (with_summary and isinstance(value, dict) and isinstance(value.get('blocks'), list) and 'summarySelection' not in value
and not any(isinstance(block, dict) and 'role' in block for block in value['blocks'])):
value = {**value, 'summarySelection': {'leadIndex': 0, 'evidenceIndexes': list(range(1, len(value['blocks'])))}}
content = value if isinstance(value, str) else json.dumps(value, ensure_ascii=False)
return io.BytesIO(json.dumps({'choices': [{'finish_reason': 'stop', 'message': {'content': content}}]}).encode())
def invoke(provider, *, docs=None, context=None, draft=None):
return synthesize_native(base_url='https://offline.invalid/v1', api_key='synthetic-fixture',
model='fixture', question='解释本期质量变化', evidence=docs or evidence(),
cancel_event=threading.Event(), context=context, opener=provider, native_answer=draft)
def request_phase(request):
assert request.get_header('X-quality-analysis-phase') == 'finalizing'
system = json.loads(request.data)['messages'][0]['content']
return 'evidence_review' if system.startswith('逐句核验证据') else 'native_synthesis'
def fixture_payload(request):
payload = json.loads(json.loads(request.data)['messages'][1]['content'])
# Legacy placeholder fixtures build their own compatibility catalog from
# the actual fact payload. Real model requests teach only natural prose.
if 'facts' in payload and 'numericTokens' not in payload:
scopes = {scope['scopeId']: scope for scope in payload.get('scopes') or []}
facts = []
for value in payload['facts']:
fact = {**value, 'scope': scopes.get(value.get('scopeId'), {}),
'verifiedDisplayValues': value.get('values') or []}
if fact.get('kind') == 'history' and fact.get('label') not in (None, '已确认历史事实'):
fact['kind'] = 'product'
facts.append(fact)
tokens = _binding_inventory(facts)
payload['numericTokens'] = {key: value for key, value in tokens.items() if key.startswith('N')}
payload['identifierTokens'] = {key: value for key, value in tokens.items() if key.startswith('I')}
return payload
def token(payload, value, fact_id='F1'):
key = next(key for key, entry in payload['numericTokens'].items()
if entry['factId'] == fact_id and entry['value'] == value)
return '{{' + key + '}}'
class NativeAnswerTests(unittest.TestCase):
def test_stage_scrap_total_cannot_bind_to_a_named_formal_category(self):
fact = {'id':'F1','kind':'stage','label':'黑片正式指标',
'text':'2026年9月黑片报废重量14.00 kg。','evidenceRef':'Q00000001',
'verifiedNativeNumericValues':[{'sourceOrdinal':0,'rawValue':'14','displayValue':'14.00',
'unit':'kg','metricField':'scrapWeight','metricRole':'scrapWeight','stageHint':'黑片'}]}
tokens = _binding_inventory([fact])
for category in ('责废','料废'):
text = f'正式{category}14.00 kg。'
parsed = _bind_literal_measures({'blocks':[{'text':text,'factIds':['F1']}]},tokens,{},facts=[fact])
with self.subTest(category=category):
self.assertEqual(parsed['blocks'][0]['text'],text)
with self.assertRaises(NativeCompositionError):
_validate_blocks(parsed,[fact],tokens)
self.assertIsNotNone(_source_relation_gap({'text':text,'factIds':['F1']},[fact]))
def test_source_proof_projects_category_total_and_missing_selected_field_separately(self):
period = {'year':2026,'month':9}
result = {'kind':'quality_investigation','action':'source','releaseId':'release-1',
'period':period,'filters':{},'stage':'black',
'current':{'context':{'releaseId':'release-1','stage':'black','year':2026,'month':9},
'totalRows':1,'aggregateComplete':True,'selectedFields':['责废尺寸重量'],
'categories':[{'category':'责废','formalWeight':'7',
'formalCoverage':{'rows':1,'numericRows':1,'keyAbsentRows':0,'nullOrBlankRows':0,
'invalidNumericRows':0,'negativeRows':0},
'fields':[{'field':'责废尺寸重量','coverage':{'rows':1,'numericRows':0,
'nullOrBlankRows':1,'keyAbsentRows':0,'invalidNumericRows':0,
'negativeRows':0,'numericSum':'3'}}]}],
'sourceVerification':{'complete':True,'allSelectedValuesMatchStored':True,
'expectedRows':1,'verifiedRows':1,'missingRows':0,'mismatchRows':0,
'files':[{'fields':[{'field':'责废尺寸重量','sourceCoverage':{'rows':1,
'numericRows':0,'nullOrBlankRows':1,'keyAbsentRows':0,'invalidNumericRows':0,
'negativeRows':0,'numericSum':'3'}}]}]}},
'comparisonPeriod':None}
document = {'releaseId':'release-1','evidenceRef':'Q00000001','mode':'investigate','result':result}
context = {'releaseId':'release-1','period':period,'filters':{},
'_agentTask':{'kind':'analysis','period':period,'comparisonPeriod':None,
'filters':{},'stages':['black'],'executionMode':'native_agent'}}
report = build_native_report([document],context,'核对责废原件和正式分量')
category = next(fact for fact in report['facts'] if fact.get('kind') == 'category')
missing = next(fact for fact in report['facts'] if fact.get('kind') == 'source_coverage')
self.assertIn('责废',category['text'])
self.assertIn('7.00 kg',category['text'])
self.assertIn('空值',missing['text'])
self.assertIn('不能按零处理',missing['text'])
self.assertNotIn('3',missing['text'])
typed = category['verifiedNativeNumericValues'][0]
self.assertEqual(typed['metricField'],'categoryWeight')
self.assertEqual(typed['categoryHint'],'责废')
source = next(fact for fact in report['facts'] if fact.get('kind') == 'source')
reconciliation = next(fact for fact in report['facts'] if fact.get('kind') == 'source_reconciliation')
self.assertIn('不证明正式类别与来源细项已对账', source['text'])
self.assertIn('未完整对账', reconciliation['text'])
self.assertFalse(reconciliation['_nativeSourceCategoryReconciliation']['reconciled'])
source_metrics = {value['metricField'] for value in source['verifiedNativeNumericValues']}
self.assertEqual(source_metrics, {'sourceVerifiedRows','sourceExpectedRows','sourceMissingRows','sourceMismatchRows'})
tokens = _binding_inventory(report['facts'])
parsed = _bind_literal_measures({'blocks':[{'text':'正式责废7.00 kg。','factIds':[category['id']]}]},
tokens,{},facts=report['facts'])
blocks, _ = _validate_blocks(parsed,report['facts'],tokens)
self.assertIn('7.00 kg',blocks[0]['text'])
self.assertIsNone(_source_relation_gap(blocks[0],report['facts']))
proof_text = '黑片原表已核验行数与应核验行数均为1,未核验0条,不一致0条。'
proof = _bind_literal_measures({'blocks':[{'text':proof_text,'factIds':[source['id']]}]},tokens,{})
self.assertEqual(proof['blocks'][0]['text'].count('{{N'),3)
proof_blocks, _ = _validate_blocks(proof,report['facts'],tokens)
self.assertIn('已核验行数与应核验行数均为1',proof_blocks[0]['text'])
self.assertIn('未核验0条,不一致0条',proof_blocks[0]['text'])
exact_proof = _bind_literal_measures({'blocks':[{'text':'已核验1行/应核验1行;未核验0条;不一致0条。',
'factIds':[source['id']]}]},tokens,{})
self.assertEqual(exact_proof['blocks'][0]['text'].count('{{N'),4)
_validate_blocks(exact_proof,report['facts'],tokens)
mismatch = _bind_literal_measures({'blocks':[{'text':'未核验1条。','factIds':[source['id']]}]},tokens,{})
with self.assertRaises(NativeCompositionError):
_validate_blocks(mismatch,report['facts'],tokens)
uneven_source = {'id':'F90','text':'已核验1行/应核验2行;未核验1条;不一致0条。',
'evidenceRef':'Q00000090','verifiedNativeNumericValues':[
{'sourceOrdinal':0,'rawValue':'1','displayValue':'1','unit':'metadata_count',
'metricField':'sourceVerifiedRows','sourceVerificationCount':True},
{'sourceOrdinal':1,'rawValue':'2','displayValue':'2','unit':'metadata_count',
'metricField':'sourceExpectedRows','sourceVerificationCount':True},
{'sourceOrdinal':2,'rawValue':'1','displayValue':'1','unit':'metadata_count',
'metricField':'sourceMissingRows','sourceVerificationCount':True},
{'sourceOrdinal':3,'rawValue':'0','displayValue':'0','unit':'metadata_count',
'metricField':'sourceMismatchRows','sourceVerificationCount':True}]}
uneven_tokens = _binding_inventory([uneven_source])
exact_uneven = _bind_literal_measures({'blocks':[{'text':'已核验1行/应核验2行;未核验1条;不一致0条。',
'factIds':['F90']}]},uneven_tokens,{})
self.assertEqual(exact_uneven['blocks'][0]['text'].count('{{N'),4)
_validate_blocks(exact_uneven,[uneven_source],uneven_tokens)
false_equal = _bind_literal_measures({'blocks':[{'text':'已核验行数与应核验行数均为1。',
'factIds':['F90']}]},uneven_tokens,{})
self.assertNotIn('{{N',false_equal['blocks'][0]['text'])
with self.assertRaises(NativeCompositionError):
_validate_blocks(false_equal,[uneven_source],uneven_tokens)
scoped_source = {'id':'F91','kind':'source','label':'黑片原表核验',
'text':'2026年9月黑片规格A已核验1行/应核验2行;未核验1条;不一致0条。',
'evidenceRef':'Q00000091','scope':{'period':{'year':2026,'month':9},
'stage':'black','filters':{'product':'规格A'}},
'verifiedNativeNumericValues':[
{'sourceOrdinal':2,'rawValue':'1','displayValue':'1','unit':'metadata_count',
'metricField':'sourceVerifiedRows','sourceVerificationCount':True,'stageHint':'黑片',
'period':{'year':2026,'month':9},'ownerScope':{'product':'规格A'}},
{'sourceOrdinal':3,'rawValue':'2','displayValue':'2','unit':'metadata_count',
'metricField':'sourceExpectedRows','sourceVerificationCount':True,'stageHint':'黑片',
'period':{'year':2026,'month':9},'ownerScope':{'product':'规格A'}},
{'sourceOrdinal':4,'rawValue':'1','displayValue':'1','unit':'metadata_count',
'metricField':'sourceMissingRows','sourceVerificationCount':True,'stageHint':'黑片',
'period':{'year':2026,'month':9},'ownerScope':{'product':'规格A'}},
{'sourceOrdinal':5,'rawValue':'0','displayValue':'0','unit':'metadata_count',
'metricField':'sourceMismatchRows','sourceVerificationCount':True,'stageHint':'黑片',
'period':{'year':2026,'month':9},'ownerScope':{'product':'规格A'}}]}
scoped_tokens = _binding_inventory([scoped_source])
def bind_scoped(text):
parsed = {'blocks':[{'text':text,'factIds':['F91']}]}
parsed = _bind_exact_periods(parsed,scoped_tokens,{})
parsed = _bind_exact_year_literals(parsed,scoped_tokens,{})
return _bind_literal_measures(parsed,scoped_tokens,{},facts=[scoped_source])
matched = bind_scoped('2026年9月黑片规格A已核验1行。')
_validate_blocks(matched,[scoped_source],scoped_tokens)
for wrong_scope in ('2026年9月亮片规格A已核验1行。',
'2026年8月黑片规格A已核验1行。',
'2026年9月黑片规格B已核验1行。'):
with self.subTest(wrong_scope=wrong_scope):
mismatch = bind_scoped(wrong_scope)
self.assertFalse(any('{{' + key + '}}' in mismatch['blocks'][0]['text']
for key, token_value in scoped_tokens.items() if token_value.get('sourceVerificationCount')))
with self.assertRaises(NativeCompositionError):
_validate_blocks(mismatch,[scoped_source],scoped_tokens)
def test_formal_category_source_relation_needs_same_scope_server_reconciliation(self):
period = {'year':2026,'month':9}
base = {'kind':'quality_investigation','action':'source','releaseId':'release-1',
'period':period,'filters':{'product':'规格06'},'stage':'black',
'current':{'context':{'releaseId':'release-1','stage':'black','year':2026,'month':9,
'product':'规格06'},
'totalRows':1,'aggregateComplete':True,'selectedFields':['责废尺寸重量'],
'categories':[{'category':'责废','formalWeight':'7','rawTotalWeight':None,
'rawTotalCoverage':{'rows':1,'numericRows':0,'keyAbsentRows':0,'nullOrBlankRows':1,
'invalidNumericRows':0,'negativeRows':0},
'formalCoverage':{'rows':1,'numericRows':1,'keyAbsentRows':0,'nullOrBlankRows':0,
'invalidNumericRows':0,'negativeRows':0},
'fields':[{'field':'责废尺寸重量','coverage':{'rows':1,'numericRows':0,
'nullOrBlankRows':1,'keyAbsentRows':0,'invalidNumericRows':0,
'negativeRows':0,'numericSum':None}}],
'subitemWeightSum':'0','differenceFormalMinusSubitems':'7',
'comparableRows':1,'missingOrInvalidFormalRows':0,'rawFormalMismatchRows':1,
'noFineFields':False,'requiresFurtherInvestigation':True}],
'sourceVerification':{'complete':True,'allSelectedValuesMatchStored':True,
'expectedRows':1,'verifiedRows':1,'missingRows':0,'mismatchRows':0}},
'comparisonPeriod':None}
document = {'releaseId':'release-1','evidenceRef':'Q00000001','mode':'investigate','result':base}
context = {'releaseId':'release-1','period':period,'filters':{'product':'规格06'},
'_agentTask':{'kind':'analysis','period':period,'comparisonPeriod':None,
'filters':{'product':'规格06'},'stages':['black'],'executionMode':'native_agent'}}
report = build_native_report([document],context,'核对规格06责废原件与正式分量')
category = next(fact for fact in report['facts'] if fact.get('kind') == 'category')
source = next(fact for fact in report['facts'] if fact.get('kind') == 'source')
fact_ids = [category['id'],source['id']]
raw_only = '责废7.00 kg;原件所选字段与存储记录匹配,但无法确认正式责废分量与来源细项相等。'
self.assertIsNone(_source_relation_gap({'text':raw_only,'factIds':fact_ids},report['facts']))
raw_label_only = '原件中的责废原始值与原表一致。'
self.assertIsNone(_source_relation_gap({'text':raw_label_only,'factIds':fact_ids},report['facts']))
raw_amount_audit = '原件中的责废原始值7.00 kg已通过原件核验。'
self.assertIsNone(_source_relation_gap({'text':raw_amount_audit,'factIds':fact_ids},report['facts']))
for negative_claim in ('正式责废重量7.00 kg未经原件核验。',
'正式责废重量7.00 kg尚未通过原件核验。'):
self.assertIsNone(_source_relation_gap({'text':negative_claim,'factIds':fact_ids},report['facts']))
explicit_difference = '正式责废重量7.00 kg,与原件细项不一致。'
self.assertIsNone(_source_relation_gap({'text':explicit_difference,'factIds':fact_ids},report['facts']))
invalid_claims = (
'正式责废重量7.00 kg,与原始Excel核验结果一致。',
'正式**责废**重量7.00 kg,与**原件**核验结果相符。',
'正式责废重量7.00 kg已通过原件核验。',
'原件核验确认正式责废重量7.00 kg。',
'责废7.00 kg已通过原件核验。',
'| 正式责废分量 | 原件对账状态 |\n|---|---|\n| 7.00 kg | 与Excel一致 |',
)
for text in invalid_claims:
with self.subTest(text=text):
self.assertIsNotNone(_source_relation_gap({'text':text,'factIds':fact_ids},report['facts']))
reconciled = copy.deepcopy(base)
category_data = reconciled['current']['categories'][0]
category_data.update(rawTotalWeight='7',subitemWeightSum='7',differenceFormalMinusSubitems='0',
rawFormalMismatchRows=0,requiresFurtherInvestigation=False,
rawTotalCoverage={'rows':1,'numericRows':1,'keyAbsentRows':0,'nullOrBlankRows':0,
'invalidNumericRows':0,'negativeRows':0},
fields=[{'field':'责废尺寸重量','coverage':{'rows':1,'numericRows':1,
'nullOrBlankRows':0,'keyAbsentRows':0,'invalidNumericRows':0,'negativeRows':0,
'numericSum':'7'}}])
reconciled_report = build_native_report([{**document,'result':reconciled}],context,
'核对规格06责废原件与正式分量')
reconciled_category = next(fact for fact in reconciled_report['facts'] if fact.get('kind') == 'category')
reconciled_source = next(fact for fact in reconciled_report['facts'] if fact.get('kind') == 'source')
self.assertTrue(reconciled_category['_nativeSourceCategoryReconciliation']['reconciled'])
valid_claim = '正式责废重量7.00 kg,与原表细项对账一致。'
self.assertIsNone(_source_relation_gap({'text':valid_claim,
'factIds':[reconciled_category['id'],reconciled_source['id']]},reconciled_report['facts']))
stage_qualified_claim = '黑片正式责废分量与原表细项对账一致。'
self.assertIsNone(_source_relation_gap({'text':stage_qualified_claim,
'factIds':[reconciled_category['id'],reconciled_source['id']]},reconciled_report['facts']))
typed_amount_claim = '责废7.00 kg已通过原件核验。'
self.assertIsNone(_source_relation_gap({'text':typed_amount_claim,
'factIds':[reconciled_category['id'],reconciled_source['id']]},reconciled_report['facts']))
independent_following_clause = '黑片正式责废分量与原表细项对账一致,综合指标仍使用正式口径。'
self.assertIsNone(_source_relation_gap({'text':independent_following_clause,
'factIds':[reconciled_category['id'],reconciled_source['id']]},reconciled_report['facts']))
for wrong_scope_claim in (
'亮片正式责废分量与原表细项对账一致。',
'黑片2026年8月正式责废分量与原表细项对账一致。'):
with self.subTest(wrong_scope_claim=wrong_scope_claim):
self.assertIsNotNone(_source_relation_gap({'text':wrong_scope_claim,
'factIds':[reconciled_category['id'],reconciled_source['id']]},
reconciled_report['facts']))
wrong_scope_source = copy.deepcopy(reconciled_source)
wrong_scope_source['_nativeSourceCategoryReconciliations'][0]['stage'] = 'bright'
wrong_scope_category = copy.deepcopy(reconciled_category)
wrong_scope_category.pop('_nativeSourceCategoryReconciliation',None)
self.assertIsNotNone(_source_relation_gap({'text':valid_claim,
'factIds':[wrong_scope_category['id'],wrong_scope_source['id']]},
[wrong_scope_category,wrong_scope_source]))
def approving_review(request, timeout):
return response({'reportTitle':'原件与正式责废核验','reviews':[{'sourceId':'D1',
'role':'core','factIds':fact_ids,'supported':True,'statisticalClaimsSupported':True}],
'missingGoalDescriptions':[],'visualIds':[]})
unsafe_drafts = (
'正式责废重量7.00 kg,与原始Excel核验结果一致。',
'正式责废重量7.00 kg已通过原件核验。',
'原件核验确认正式责废重量7.00 kg。',
'责废7.00 kg已通过原件核验。',
)
for draft in unsafe_drafts:
with self.subTest(reviewed_draft=draft), patch('nao_native_answer.build_native_report',
return_value=copy.deepcopy(report)):
reviewed = synthesize_native_answer(base_url='https://offline.invalid/v1',api_key='fixture',
model='fixture',question='核对规格06责废正式分量与原件',evidence=[],
cancel_event=threading.Event(),context={'_nativeExecutionId':'exec',
'_dshDraftReview':True,'_autonomousAnswer':True,
'_agentTask':{'kind':'analysis','period':period,'filters':{'product':'规格06'},
'stages':['black']}},opener=approving_review,
native_answer='## 重点\n\n'+draft)
self.assertNotIn(draft,reviewed['answer'])
self.assertTrue(reviewed['compositionDiagnostics'].get('sourceRelationGaps'))
with patch('nao_native_answer.build_native_report',return_value=copy.deepcopy(report)):
negative_reviewed=synthesize_native_answer(base_url='https://offline.invalid/v1',api_key='fixture',
model='fixture',question='核对规格06责废正式分量与原件',evidence=[],
cancel_event=threading.Event(),context={'_nativeExecutionId':'exec',
'_dshDraftReview':True,'_autonomousAnswer':True,
'_agentTask':{'kind':'analysis','period':period,'filters':{'product':'规格06'},
'stages':['black']}},opener=approving_review,
native_answer='## 重点\n\n正式责废重量7.00 kg未经原件核验。')
self.assertFalse(negative_reviewed['compositionDiagnostics'].get('sourceRelationGaps'))
def test_native_review_repair_requires_remainder_verdicts_and_nonoverlapping_clause_ids(self):
fact = {'id':'F1','kind':'stage','label':'黑片正式指标',
'text':'黑片正式报废重量14.00 kg。','evidenceRef':'Q00000001',
'verifiedNativeNumericValues':[{'sourceOrdinal':0,'rawValue':'14','displayValue':'14.00',
'unit':'kg','metricField':'scrapWeight','stageHint':'黑片',
'period':{'year':2026,'month':9}}]}
report = {'facts':[fact],'missingEvidence':[],'requiredFactIds':[],'priorityFactIds':[],
'scopes':[],'sections':[],'_presentationSources':[],'rejectedEvidenceRefs':[],
'notices':[],'calculationRules':[],'metricFormulaDescriptions':[],
'unverifiedEvidenceWarnings':[]}
draft = '## 重点\n\n黑片正式报废重量14.00 kg。无法支持的观察句。'
calls = []
unsupported = '无法支持的观察句。'
def provider(request, timeout):
messages = json.loads(request.data)['messages']
payload = json.loads(messages[1]['content'])
calls.append(messages)
block = payload['draftBlocks'][0]
bad_id = next(item['clauseId'] for item in block['clauseCatalog']
if item.get('text') == unsupported)
if len(calls) == 1:
parent_id = next(item['clauseId'] for item in block['clauseCatalog']
if item.get('kind') == 'block')
# Parent and child overlap, and the required remainder verdicts
# are deliberately missing to exercise the bounded repair.
row = {'sourceId':block['sourceId'],'role':'core','factIds':['F1'],
'supported':False,'statisticalClaimsSupported':False,
'unsupportedClauseIds':[parent_id,bad_id]}
return response({'reportTitle':'黑片报废依据','reviews':[row],
'missingGoalDescriptions':[],'visualIds':[]})
row = {'sourceId':block['sourceId'],'role':'core','factIds':['F1'],
'supported':False,'statisticalClaimsSupported':False,'unsupportedClauseIds':[bad_id],
'remainingClaimsSupported':True,'remainingStatisticalClaimsSupported':True}
return response({'reportTitle':'黑片报废依据','reviews':[row],
'missingGoalDescriptions':[],'visualIds':[]})
with patch('nao_native_answer.build_native_report',return_value=copy.deepcopy(report)):
final = synthesize_native_answer(base_url='https://offline.invalid/v1',api_key='fixture',
model='fixture',question='解释黑片正式报废重量',evidence=[],cancel_event=threading.Event(),
context={'_nativeExecutionId':'exec','_dshDraftReview':True,
'_agentTask':{'kind':'analysis','period':{'year':2026,'month':9},
'filters':{},'stages':['black']}},opener=provider,native_answer=draft)
self.assertEqual(len(calls),2)
self.assertTrue(final['compositionDiagnostics'].get('reviewClauseRemovals'))
self.assertNotIn(unsupported,final['answer'])
self.assertIn('14.00 kg',final['answer'])
for field in ('remainingClaimsSupported','remainingStatisticalClaimsSupported'):
self.assertIn(field,calls[0][0]['content'])
self.assertIn(field,calls[1][-1]['content'])
self.assertIn('互不重叠',calls[1][-1]['content'])
self.assertIn('父节点与其子句/表格行不可同时选择',calls[1][-1]['content'])
def test_pp_table_headers_bind_same_stage_roles_when_rounded_values_collide(self):
from nao_native_answer import (_native_atomic_inventory,_native_unique_span,
_native_resolve_span,_prepare_native_draft,_native_draft_review_messages)
period={'year':2026,'month':9}
facts=[
{'id':'F1','kind':'stage','label':'黑片自身率变化',
'text':'黑片自身率上升0.15个百分点。','evidenceRef':'Q00000011',
'scope':{'period':period,'filters':{},'stage':'black'},
'verifiedNativeNumericValues':[{'sourceOrdinal':0,'rawValue':'0.14687895375728394',
'displayValue':'0.15','unit':'pp','metricField':'rateChange',
'metricRole':'OwnStageRateChangePp','stageHint':'黑片','period':period,'ownerScope':{}}]},
{'id':'F2','kind':'stage','label':'黑片对综合率贡献',
'text':'黑片对综合率变化贡献为0.15个百分点。','evidenceRef':'Q00000012',
'scope':{'period':period,'filters':{},'stage':'black'},
'verifiedNativeNumericValues':[{'sourceOrdinal':0,'rawValue':'0.14521243189527',
'displayValue':'0.15','unit':'pp','metricField':'stageContribution',
'metricRole':'ContributionPp','stageHint':'黑片','period':period,'ownerScope':{}}]},
]
context={'_agentTask':{'kind':'analysis','period':period,'filters':{},'stages':['black']}}
draft=('| 阶段 | 自身率变化 (pp) | 对综合率贡献 (pp) |\n'
'|---|---:|---:|\n| 黑片 | 0.15 | 0.15 |')
prepared,metadata=_prepare_native_draft(draft,facts,context)
self.assertEqual(metadata['protectedMetricSpans'],2)
by_start={item['start']:item for item in prepared[0]['_protected']}
own_start=draft.index('0.15')
contribution_start=draft.rindex('0.15')
self.assertEqual(by_start[own_start]['factId'],'F1')
self.assertEqual(by_start[contribution_start]['factId'],'F2')
tokens=_native_atomic_inventory(facts)
fact_map={fact['id']:fact for fact in facts}
own_token=next(token for token in tokens.values() if token.get('metricRole')=='OwnStageRateChangePp')
contribution_token=next(token for token in tokens.values() if token.get('metricRole')=='ContributionPp')
for start,correct,wrong in ((own_start,own_token,contribution_token),
(contribution_start,contribution_token,own_token)):
match=re.match(r'0\.15',draft[start:])
self.assertIsNotNone(_native_resolve_span(draft,start,start+len(match[0]),correct,
fact_map,tokens,context=context,require_positive=True))
self.assertIsNone(_native_resolve_span(draft,start,start+len(match[0]),wrong,
fact_map,tokens,context=context,require_positive=True))
for wrong_stage in ('亮片','综合'):
wrong=draft.replace('| 黑片 |','| '+wrong_stage+' |')
self.assertIsNone(_native_unique_span(wrong,re.search(r'0\.15',wrong),fact_map,tokens,context,True))
messages=_native_draft_review_messages(prepared,facts,'说明黑片自身率变化与综合贡献',context,{}, {})
payload=json.loads(messages[1]['content'])
catalog=payload['bindingCatalog']
for token in (own_token,contribution_token):
item=catalog[next(key for key,value in catalog.items() if value['factId']==token['factId'])]
self.assertEqual(item['metricRole'],token['metricRole'])
self.assertEqual(item['metricField'],token['metricField'])
self.assertEqual(item['stage'],'黑片')
self.assertEqual(item['ownerScope'],{})
self.assertEqual(item['period'],period)
self.assertNotEqual(own_token['rawValue'],contribution_token['rawValue'])
def test_dsh_draft_review_flag_preserves_draft_and_unflagged_execution_keeps_presentation(self):
fact = {'id':'F1','kind':'stage','label':'黑片正式指标','text':'黑片报废重量14.00 kg。',
'evidenceRef':'Q00000001','verifiedNativeNumericValues':[{'sourceOrdinal':0,
'rawValue':'14','displayValue':'14.00','unit':'kg','metricField':'scrapWeight',
'metricRole':'scrapWeight','stageHint':'黑片','period':{'year':2026,'month':9}}]}
report = {'facts':[fact],'missingEvidence':[],'requiredFactIds':[],'priorityFactIds':[],
'scopes':[],'sections':[],'_presentationSources':[{'markdown':'### 黑片\n\n报废重量14.00 kg。',
'evidenceRefs':['Q00000001'],'scope':{}}]}
with patch('nao_native_answer.build_native_report',return_value=copy.deepcopy(report)), \
patch('nao_native_answer._synthesize_verified_report',return_value={'answer':'checked'}) as finalize:
synthesize_native_answer(base_url='https://offline.invalid',api_key='fixture',model='fixture',
question='解释黑片报废重量',evidence=[],cancel_event=threading.Event(),
context={'_nativeExecutionId':'exec','_dshDraftReview':True,
'_agentTask':{'kind':'analysis'}},native_answer='## 重点\n\n黑片报废重量14.00 kg。')
self.assertEqual(finalize.call_args.kwargs['native_answer'],'## 重点\n\n黑片报废重量14.00 kg。')
self.assertNotIn('presentationSections',finalize.call_args.kwargs['report'])
with patch('nao_native_answer.build_native_report',return_value=copy.deepcopy(report)), \
patch('nao_native_answer.native_sections',return_value=['### 黑片\n\n报废重量14.00 kg。']), \
patch('nao_native_answer._synthesize_verified_report',return_value={'answer':'checked'}) as finalize:
synthesize_native_answer(base_url='https://offline.invalid',api_key='fixture',model='fixture',
question='解释黑片报废重量',evidence=[],cancel_event=threading.Event(),
context={'_nativeExecutionId':'exec','_agentTask':{'kind':'analysis'}},
native_answer='## 重点\n\n黑片报废重量14.00 kg。')
self.assertIsNone(finalize.call_args.kwargs['native_answer'])
self.assertTrue(finalize.call_args.kwargs['report']['presentationSections'])
def test_stage_contribution_shorthand_binds_only_the_named_stage_and_contribution_role(self):
from nao_native_answer import _native_atomic_inventory, _native_resolve_span
period={'year':2026,'month':10}
facts=[]
for fact_id,field,role,raw,label in [('F1','stageContribution','ContributionPp','-0.14687895375728394','对综合率变化贡献'),
('F2','rateChange','OwnStageRateChangePp','-0.14841364717368','自身率变化')]:
facts.append({'id':fact_id,'kind':'stage','label':'黑片'+label,'text':'黑片'+label+'-0.15个百分点。',
'evidenceRef':'Q00000001','scope':{'period':period,'filters':{},'stage':'black'},
'verifiedNativeNumericValues':[{'sourceOrdinal':0,'rawValue':raw,'displayValue':'-0.15',
'unit':'pp','metricField':field,'metricRole':role,'stageHint':'黑片','period':period,'ownerScope':{}}]})
tokens=_native_atomic_inventory(facts);known={f['id']:f for f in facts}
contribution=next(t for t in tokens.values() if t.get('metricRole')=='ContributionPp')
own=next(t for t in tokens.values() if t.get('metricRole')=='OwnStageRateChangePp')
context={'_agentTask':{'kind':'analysis','period':period,'filters':{},'stages':['black'],'fields':['rate']}}
text='黑片阶段贡献约 -0.15 pp。';match=re.search('-0.15',text)
self.assertIsNotNone(_native_resolve_span(text,*match.span(),contribution,known,tokens,context=context,require_positive=True))
self.assertIsNone(_native_resolve_span(text,*match.span(),own,known,tokens,context=context,require_positive=True))
for wrong in ('亮片阶段贡献约 -0.15 pp。','黑片对亮片合格率变化贡献约 -0.15 pp。'):
match=re.search('-0.15',wrong)
self.assertIsNone(_native_resolve_span(wrong,*match.span(),contribution,known,tokens,context=context,require_positive=True))
def test_native_formal_presentation_gets_one_bounded_repair_before_independent_review(self):
period={'year':2026,'month':10}
fact={'id':'F1','kind':'stage','label':'黑片合格率','text':'黑片合格率98.84%。','evidenceRef':'Q00000001',
'scope':{'period':period,'filters':{},'stage':'black'}}
report={'facts':[fact],'missingEvidence':[],'requiredFactIds':['F1'],'priorityFactIds':['F1'],'scopes':[],
'sections':[],'notices':[],'rejectedEvidenceRefs':[],'_presentationSources':[{'markdown':'### 黑片合格率\n\n| 指标 | 正式值 |\n|---|---|\n| 黑片合格率 | 98.84% |',
'evidenceRefs':['Q00000001'],'scope':{'period':period,'stage':'black'}}]}
phases=[];generations=[]
def provider(request,timeout):
body=json.loads(request.data)
if body['messages'][0]['content'].startswith('逐句核验证据'):
phases.append('evidence_review')
return response({'reviews':[{'supported':True,'statisticalClaimsSupported':True}],'missingGoalDescriptions':[]})
phases.append('native_synthesis');generations.append(body)
if len(generations)==2:
self.assertIn('校验错误',body['messages'][-1]['content'])
self.assertNotIn('{{N',body['messages'][-1]['content'])
value='99.99' if len(generations)==1 else '98.84'
return response({'reportTitle':'2026年10月黑片合格率核对','blocks':[{'role':'core',
'text':'黑片合格率'+value+'%。','factIds':['F1']}],'visualIds':[]})
with patch('nao_native_answer.build_native_report',return_value=report):
result=synthesize_native_answer(base_url='https://offline.invalid',api_key='fixture',model='fixture',
question='生成黑片合格率报告',evidence=[],cancel_event=threading.Event(),opener=provider,
context={'_nativeExecutionId':'exec','_agentTask':{'kind':'analysis','period':period,
'filters':{},'fields':['rate'],'stages':['black']}},native_answer='未核验的调查草稿')
self.assertTrue(result['evidenceSufficient'],result['compositionDiagnostics'])
self.assertEqual(phases,['native_synthesis','native_synthesis','evidence_review'])
self.assertNotIn('99.99',result['answer'])
self.assertIn('98.84%',result['answer'])
def test_dsh_draft_cannot_keep_category_value_from_stage_total_after_review_true(self):
fact = {'id':'F1','kind':'stage','label':'黑片正式指标','text':'黑片报废重量14.00 kg。',
'evidenceRef':'Q00000001','verifiedNativeNumericValues':[{'sourceOrdinal':0,
'rawValue':'14','displayValue':'14.00','unit':'kg','metricField':'scrapWeight',
'metricRole':'scrapWeight','stageHint':'黑片','period':{'year':2026,'month':9}}]}
category_fact = {'id':'F2','kind':'category','label':'责废',
'text':'黑片正式大类分量:责废为7.00 kg。','evidenceRef':'Q00000002',
'verifiedNativeNumericValues':[{'sourceOrdinal':0,'rawValue':'7','displayValue':'7.00',
'unit':'kg','metricField':'categoryWeight','metricRole':'categoryWeight',
'stageHint':'黑片','categoryHint':'责废','period':{'year':2026,'month':9}}]}
report = {'facts':[fact,category_fact],'missingEvidence':[],'requiredFactIds':[],'priorityFactIds':[],
'scopes':[],'sections':[],'_presentationSources':[],'rejectedEvidenceRefs':[],
'notices':[],'calculationRules':[],'metricFormulaDescriptions':[],
'unverifiedEvidenceWarnings':[]}
context = {'_nativeExecutionId':'exec','_dshDraftReview':True,
'_autonomousAnswer':True,'_agentTask':{'kind':'analysis','period':{'year':2026,'month':9},
'filters':{},'stages':['black']}}
invalid_drafts = ('黑片正式责废重量为**14.00 kg**。',
'黑片正式**责废**重量为14.00 kg。', '黑片正式责废(14.00 kg)。')
for draft in invalid_drafts:
def provider(request, timeout):
return response({'reportTitle':'黑片报废依据','reviews':[{'sourceId':'D1','role':'core',
'factIds':['F1'],'supported':True,'statisticalClaimsSupported':True}],
'missingGoalDescriptions':[],'visualIds':[]})
with self.subTest(draft=draft), patch('nao_native_answer.build_native_report',
return_value=copy.deepcopy(report)):
final = synthesize_native_answer(base_url='https://offline.invalid/v1',api_key='fixture',
model='fixture',question='核对黑片责废重量',evidence=[],cancel_event=threading.Event(),
context=context,opener=provider,native_answer=draft)
self.assertEqual(final['compositionDiagnostics']['compositionMode'],'native_draft_review')
normalized_answer = re.sub(r'[*_`()()\[\]【】]', '', final['answer'])
self.assertNotRegex(normalized_answer, r'责废[^。;\n]{0,20}14\.00\s*kg')
valid_draft = '黑片正式责废重量为7.00 kg。'
def valid_provider(request, timeout):
return response({'reportTitle':'责废正式分量','reviews':[{'sourceId':'D1','role':'core',
'factIds':['F2'],'supported':True,'statisticalClaimsSupported':True}],
'missingGoalDescriptions':[],'visualIds':[]})
with patch('nao_native_answer.build_native_report',return_value=copy.deepcopy(report)):
accepted = synthesize_native_answer(base_url='https://offline.invalid/v1',api_key='fixture',
model='fixture',question='核对黑片责废重量',evidence=[],cancel_event=threading.Event(),
context=context,opener=valid_provider,native_answer=valid_draft)
self.assertTrue(accepted['evidenceSufficient'],accepted['compositionDiagnostics'])
self.assertIn('7.00 kg',accepted['answer'])
tokens = _binding_inventory([fact,category_fact])
table_cases = (
('| 对象 | 责废重量(kg) |\n|---|---:|\n| 黑片 | 14.00 |', ['F1'], False),
('| 大类 | 黑片报废重量(kg) |\n|---|---:|\n| 责废 | 7.00 |', ['F2'], True),
)
for table, fact_ids, supported in table_cases:
with self.subTest(table=table):
parsed = _bind_literal_measures({'blocks':[{'text':table,'factIds':fact_ids}]},tokens,{},facts=[fact,category_fact])
if supported:
validated, _ = _validate_blocks(parsed,[fact,category_fact],tokens)
self.assertIn('7.00',validated[0]['text'])
self.assertIsNone(_source_relation_gap({'text':validated[0]['text'],'factIds':fact_ids},[fact,category_fact]))
else:
self.assertNotIn('{{N',parsed['blocks'][0]['text'])
with self.assertRaises(NativeCompositionError):
_validate_blocks(parsed,[fact,category_fact],tokens)
def test_short_markdown_tables_and_verified_chart_selection_survive_finalization(self):
report = {'facts': [{'id': 'F1', 'text': '合格率95.00%。', 'evidenceRef': 'Q00000001'}],
'requiredFactIds': ['F1'], 'priorityFactIds': [], 'scopes': [],
'missingEvidence': [], 'rejectedEvidenceRefs': []}
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'index': i, 'supported': True, 'statisticalClaimsSupported': True}
for i in range(2)], 'missingGoalDescriptions': []})
return response({'blocks': [
{'role': 'core', 'text': '## 结论\n合格率95.00%。', 'factIds': ['F1']},
{'role': 'key_evidence', 'text': '### 指标对比\n\n| 指标 | 本期 |\n| --- | --- |\n| 合格率 | 95.00% |', 'factIds': ['F1']}],
'visualIds': ['verified-chart', 'other-source', 'invented']})
context = {'_autonomousAnswer': True, '_platformVisuals': {'visualState': 'ready', 'charts': [
{'id': 'verified-chart', 'evidenceRefs': ['Q00000001']},
{'id': 'other-source', 'evidenceRefs': ['Q00000002']}]}}
with patch('nao_native_answer.build_native_report', return_value=report):
result = invoke(provider, context=context)
self.assertTrue(result['evidenceSufficient'])
self.assertIn('| 合格率 | 95.00% |', result['readingSummary']['evidence'][0])
self.assertEqual(result['readingSummary']['visualIds'], ['verified-chart'])
self.assertFalse(result['compositionRepairAttempted'])
def test_data_presentation_repair_request_carries_natural_names_and_structure_contract(self):
from nao_native_answer import _synthesize_verified_report
fact = {'id':'F1','kind':'product','label':'产品04',
'text':'2026年9月产品04综合投入重量120.00 kg。','evidenceRef':'Q00000001',
'scope':{'period':{'year':2026,'month':9},'filters':{'product':'产品04'},
'dimension':'product','stage':'overall'}}
report = {'facts':[fact],'requiredFactIds':['F1'],'priorityFactIds':[],'scopes':[],
'missingEvidence':[],'rejectedEvidenceRefs':[],'notices':[],
'presentationSections':[{'markdown':'### 第一章:重点规格\n\n| 规格 | 综合投入重量 |\n|---|---:|\n| 产品04 | 120.00 kg |',
'factIds':['F1'],'evidenceRefs':['Q00000001'],'scope':fact['scope']}]}
calls = []
def provider(request, timeout):
messages = json.loads(request.data)['messages']
calls.append(messages)
if len(calls) == 1:
return response({'reportTitle':'质量分析简报','summarySelection':{'leadIndex':0,'evidenceIndexes':[]},
'blocks':[{'role':'core','text':'产品04综合投入重量{{N30}} kg。','factIds':['F1']}],
'visualIds':[]})
return response({'reportTitle':'质量分析简报','summarySelection':{'leadIndex':0,'evidenceIndexes':[]},
'blocks':[{'role':'core','text':'{{F1}}','factIds':['F1']}],'visualIds':[]})
result = _synthesize_verified_report(base_url='https://offline.invalid/v1',api_key='fixture',model='fixture',
question='按用户指定三章结构分析产品04。',report=report,cancel_event=threading.Event(),
context={'_agentTask':{'kind':'analysis'}},opener=provider)
self.assertEqual(len(calls),2)
self.assertTrue(result['compositionRepairAttempted'])
generation_rules = calls[0][0]['content']
repair_rules = calls[1][-1]['content']
for instruction in ('平台会按已核验来源自动插入完整数据表','不重复列出全部投入规模、分母',
'产品、组织和客户必须写完整原名','保留用户明确指定的章节数量、顺序和标题',
'简短概括放在第一章内','每章Markdown标题与该章正文必须放在同一个block',
'不能把标题单独做成factIds为空的block','N/I/P开头的内部诊断编号'):
self.assertIn(instruction,generation_rules)
for instruction in ('平台会插入完整已核验排名表与投入/分母数据','避免在text中逐行重述排名或重复完整规模/分母',
'产品、组织和客户写完整原名','保留用户指定的章节数量、顺序、标题',
'简短概括并入第一章','每章Markdown标题与该章正文必须留在同一个block',
'不要保留空factIds标题','N/I/P编号只是定位问题的诊断线索'):
self.assertIn(instruction,repair_rules)
self.assertIn('校验错误中的N/I/P编号只是定位问题的诊断线索',repair_rules)
self.assertIn('不可用编号替代产品名、组织名或指标名',repair_rules)
self.assertIn('保留用户指定的章节数量、顺序、标题',repair_rules)
self.assertNotIn('N30',repair_rules)
self.assertNotIn('改用已有证据的token',repair_rules)
self.assertTrue(result['evidenceSufficient'],result['compositionDiagnostics'])
def test_strong_defect_comparison_needs_same_scope_counterpart_and_deletes_only_clause(self):
scope = {'filters': {'department': '新能源汽车磁钢'}, 'period': {'year': 2026, 'month': 9},
'comparisonPeriod': {'year': 2026, 'month': 8}, 'dimension': 'defect', 'stage': 'black'}
facts = [{'id': 'F1', 'kind': 'defect', 'label': '料废缺角重量', 'text': '黑片来源细项料废缺角重量:7,624.45→10,354.03 kg,增减+2,729.58 kg。',
'evidenceRef': 'Q00000001', 'scope': scope},
{'id': 'F2', 'kind': 'defect', 'label': '料废尺寸重量', 'text': '黑片来源细项料废尺寸重量:25.02→290.87 kg,增减+265.85 kg。',
'evidenceRef': 'Q00000002', 'scope': {**scope, 'filters': {'product': '规格甲', 'department': '新能源汽车磁钢'}}}]
text = '料废缺角重量从7,624.45 kg增至10,354.03 kg(+2,729.58 kg),规模远超其他分组,是本轮优先核查线索。规格甲料废尺寸重量从25.02 kg增至290.87 kg(+265.85 kg),仍需核查生产原因。'
report = {'facts': facts, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': text, 'factIds': ['F1', 'F2']}]})
with patch('nao_native_answer.build_native_report', return_value=report): final = invoke(provider)
self.assertNotIn('规模远超其他分组', final['answer'])
self.assertIn('是本轮优先核查线索', final['answer'])
self.assertIn('规格甲料废尺寸重量从25.02 kg增至290.87 kg', final['answer'])
self.assertEqual(final['compositionDiagnostics']['sourceClauseRemovals'][0]['clauses'], ['规模远超其他分组'])
comparable = copy.deepcopy(facts); comparable[1]['scope'] = scope
self.assertIsNone(_source_relation_gap({'text': text, 'factIds': ['F1', 'F2']}, comparable))
def test_full_paired_response_runner_up_is_optional_context_without_new_query(self):
from test_nao_query_plan_defects_report_visuals import defect_result, document, source_field, java_category_reconciliation, compact_comparison_result, RELEASE
from nao_quality_tools import QualityTools
fields = [source_field('责废尺寸重量'), source_field('责废缺角重量')]
result = defect_result(comparison=True, fields=fields)
result['rows'] = result['rows'][:1]; result.update(totalRows=2, rowsReturned=1, truncated=True, pairedTotalRows=2)
result['pairedRows'] = [{'key': field['key'], 'current': {'key': field['key'], 'label': field['label'], 'category': '责废', 'weight': 4.5},
'comparison': {'key': field['key'], 'label': field['label'], 'category': '责废', 'weight': 3.0},
'present': {'current': True, 'comparison': True}, 'deltaWeight': 1.5, 'deltaAvailable': True} for field in fields]
result['comparisonCategoryReconciliation'] = [java_category_reconciliation(fields, source_rows=3,
field_weights={field['key']: 3 for field in fields})]
original = copy.deepcopy(result)
report = build_native_report([document(result)], context={'releaseId': RELEASE,
'_agentTask': {'executionMode': 'native_agent'}})
self.assertFalse(report['missingEvidence'])
context_fact = next(fact for fact in report['facts'] if fact['kind'] == 'defect_context')
self.assertEqual(context_fact['label'], '责废缺角重量')
self.assertIn('次位量级对照', context_fact['text'])
self.assertEqual(context_fact['evidenceRef'], 'QABCDEF01')
self.assertNotIn(context_fact['id'], report['requiredFactIds'])
self.assertEqual(result, original)
wire = QualityTools._model_view(compact_comparison_result(result))
self.assertNotIn('pairedRows', wire)
self.assertIn('defectColumns', wire)
wire_copy = copy.deepcopy(wire)
report = build_native_report([document(wire)], context={'releaseId': RELEASE,
'_agentTask': {'executionMode': 'native_agent'}})
self.assertFalse(report['missingEvidence'])
context_fact = next(fact for fact in report['facts'] if fact['kind'] == 'defect_context')
self.assertEqual(context_fact['label'], '责废缺角重量')
tokens = _binding_inventory(report['facts'])
for value, valid in (('1.50 kg', True), ('1.51 kg', False), ('1.50吨', False)):
parsed = _bind_literal_measures({'blocks': [{'text': '责废缺角重量增减'+value+'。', 'factIds': [context_fact['id']]}]}, tokens, {})
if valid: _validate_blocks(parsed, report['facts'], tokens)
else:
with self.assertRaises(NativeCompositionError): _validate_blocks(parsed, report['facts'], tokens)
self.assertEqual(wire, wire_copy)
# A compact wire response with an actual Top 2 must not silently become
# Top 1 just because the presentation projection omitted result.rows.
complete_wire = copy.deepcopy(wire)
complete_wire.update(rowsReturned=2, truncated=False)
report = build_native_report([document(complete_wire)], context={'releaseId': RELEASE,
'_agentTask': {'executionMode': 'native_agent'}})
self.assertFalse(report['missingEvidence'])
paired_facts = [fact for fact in report['facts'] if fact['kind'] == 'defect' and '来源细项'+str(fact['label'])+':' in fact['text']]
self.assertEqual([fact['label'] for fact in paired_facts], ['责废尺寸重量', '责废缺角重量'])
self.assertFalse(any(fact['kind'] == 'defect_context' for fact in report['facts']))
def test_exact_changes_raw_alias_matches_formal_display_without_precision_guessing(self):
facts = [{'id': 'F1', 'kind': 'product', 'label': '规格甲', 'text': '规格甲合格率95.00%。', 'scrapDelta': 758.7594,
'evidenceRef': 'Q00000001', 'validity': {'basis': 'quality_changes'}},
{'id': 'F2', 'text': '其他合格率90.00%。', 'evidenceRef': 'Q00000002'}]
tokens = _binding_inventory(facts)
for text, ids, allowed in (('报废增量758.7594。', ['F1'], True), ('报废增量758.759400 kg。', ['F1'], True),
('报废增量758.76。', ['F1'], True), ('报废增量758.7593 kg。', ['F1'], False),
('报废增量759 kg。', ['F1'], False), ('报废增量758.7594吨。', ['F1'], False),
('报废增量758.7594。', ['F2'], False)):
parsed = _bind_literal_measures({'blocks': [{'text': text, 'factIds': ids}]}, tokens, {})
with self.subTest(text=text, ids=ids):
if allowed:
blocks, _ = _validate_blocks(parsed, facts, tokens)
self.assertIn('758.76 kg', blocks[0]['text'])
else:
with self.assertRaises(NativeCompositionError): _validate_blocks(parsed, facts, tokens)
self.assertEqual(facts[0]['scrapDelta'], 758.7594)
def test_optional_claim_detail_is_inference_and_still_reviewed_unknown_rejected(self):
for claim, valid in (('detail', True), ('table', False)):
phases = []
def provider(request, timeout):
phases.append(request_phase(request))
if phases[-1] == 'evidence_review':
payload = json.loads(json.loads(request.data)['messages'][1]['content'])
self.assertEqual(payload['blocks'][0]['claimType'], 'inference')
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': '本期综合合格率95.00%。', 'factIds': ['F1'], 'claimType': claim}]})
with self.subTest(claim=claim):
final = invoke(provider, context={'_autonomousAnswer': True})
self.assertEqual(final['evidenceSufficient'], valid)
self.assertEqual('evidence_review' in phases, valid)
if valid: self.assertTrue(final['compositionDiagnostics']['claimTypeNormalizations'])
def test_formal_negative_delta_allows_exact_absolute_decrease_display_only(self):
facts = [{'id': 'F1', 'text': '亮片镀废3,882.34→3,809.94 kg,增减-72.40 kg。', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'text': '本期报废10.00 kg。', 'evidenceRef': 'Q00000002'}]
tokens = _binding_inventory(facts)
variant = next(value for value in tokens.values() if value.get('displayDirection') == 'decrease')
self.assertEqual(variant['sourceValue'], '-72.40')
self.assertEqual(variant['value'], '72.40')
for text, ids, allowed in (('镀废减少72.40 kg。', ['F1'], True), ('镀废减72.4 kg。', ['F1'], True),
('镀废下降72.40 kg。', ['F1'], True), ('镀废增加72.40 kg。', ['F1'], False),
('镀废减少72.41 kg。', ['F1'], False), ('镀废减少72 kg。', ['F1'], False),
('镀废减少72.40吨。', ['F1'], False), ('镀废减少72.40 kg。', ['F2'], False)):
with self.subTest(text=text, ids=ids):
parsed = _bind_literal_measures({'blocks': [{'text': text, 'factIds': ids}]}, tokens, {})
if allowed:
blocks, _ = _validate_blocks(parsed, facts, tokens)
self.assertIn('72.40 kg', blocks[0]['text']); self.assertNotIn('-72.40', blocks[0]['text'])
else:
with self.assertRaises(NativeCompositionError): _validate_blocks(parsed, facts, tokens)
key = next(key for key, value in tokens.items() if value.get('displayDirection') == 'decrease')
with self.assertRaises(NativeCompositionError):
_validate_blocks({'blocks': [{'text': '增加{{'+key+'}} kg。', 'factIds': ['F1']}]}, facts, tokens)
def test_same_paragraph_prior_full_product_allows_unique_ascii_short_name_only(self):
full = 'R127.985X57.516X15.786X75.485X32.232°X5.5'
facts = [{'id': 'F1', 'kind': 'product', 'label': full, 'text': full+'综合合格率82.19%。', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'kind': 'product', 'label': 'R127.111X20', 'text': 'R127.111X20综合合格率82.19%。', 'evidenceRef': 'Q00000002'}]
tokens = _binding_inventory(facts)
for text, ids, allowed in ((full+'已列明,R127规格综合率82.19%。', ['F1'], True),
(full+'已列明,R127规格综合率82.19%。', ['F1', 'F2'], False),
('R127规格综合率82.19%。'+full+'随后列明。', ['F1'], False),
('R127规格综合率82.19%。', ['F1'], False),
(full+'已列明,R128规格综合率82.19%。', ['F1'], False),
(full+'已列明,127规格综合率82.19%。', ['F1'], False)):
with self.subTest(text=text, ids=ids):
diagnostics = {}
parsed = _bind_exact_identifiers({'blocks': [{'text': text, 'factIds': ids}]}, tokens, diagnostics)
parsed = _bind_literal_measures(parsed, tokens, diagnostics)
if allowed:
blocks, _ = _validate_blocks(parsed, facts, tokens)
self.assertEqual(blocks[0]['text'].count(full), 2)
self.assertTrue(diagnostics['identifierAbbreviations'])
else:
with self.assertRaises(NativeCompositionError): _validate_blocks(parsed, facts, tokens)
def test_deleted_unique_required_fact_does_not_survive_in_refs_or_completion(self):
facts = [{'id': 'F1', 'kind': 'overall', 'text': '总体合格率95.00%。', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'kind': 'defect', 'label': '料废缺角', 'text': '料废缺角10.00 kg。', 'evidenceRef': 'Q00000002'}]
report = {'facts': facts, 'requiredFactIds': ['F1', 'F2'], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'supported': False, 'statisticalClaimsSupported': False,
'unsupportedClauses': ['料废缺角10.00 kg证明设备故障。'], 'remainingClaimsSupported': True,
'remainingStatisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': '总体合格率95.00%。料废缺角10.00 kg证明设备故障。', 'factIds': ['F1', 'F2']}]})
with patch('nao_native_answer.build_native_report', return_value=report): final = invoke(provider, context={'_autonomousAnswer': True})
self.assertFalse(final['evidenceSufficient'])
self.assertEqual(final['usedEvidenceRefs'], ['Q00000001'])
self.assertEqual(final['readingSummary']['evidenceRefs'], ['Q00000001'])
self.assertTrue(final['compositionDiagnostics']['coreGoalGaps'])
shared = [{'id': 'F1', 'kind': 'product', 'label': '规格甲', 'text': '规格甲合格率0.00%。', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'kind': 'product', 'label': '规格乙', 'text': '规格乙合格率0.00%。', 'evidenceRef': 'Q00000002'}]
remainder = _review_supported_remainder({'text': '规格甲合格率0.00%。规格乙合格率0.00%证明设备故障。', 'factIds': ['F1', 'F2']},
{'unsupportedClauses': ['规格乙合格率0.00%证明设备故障。'], 'remainingClaimsSupported': True,
'remainingStatisticalClaimsSupported': True}, shared)
self.assertEqual(remainder['factIds'], ['F1'])
self.assertEqual(remainder['evidenceRefs'], ['Q00000001'])
def test_known_bad_contribution_and_unknown_delta_clauses_are_removed_locally(self):
facts = [{'id': 'F1', 'kind': 'group', 'label': '国内', 'text': '国内合格率下降0.62个百分点,报废增量5572.52 kg。', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'kind': 'group', 'label': '海外', 'text': '海外合格率下降0.09个百分点,报废增量738.91 kg。', 'evidenceRef': 'Q00000001'},
{'id': 'F3', 'kind': 'product', 'label': '规格乙', 'scrapDelta': None, 'text': '规格乙报废重量—→5.36 kg;基期缺失。', 'evidenceRef': 'Q00000002'}]
for fact in facts[:2]: fact['validity'] = {'basis': 'quality_changes'}
report = {'facts': facts, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
def provider(request, timeout):
payload = json.loads(json.loads(request.data)['messages'][1]['content'])
self.assertEqual(next(fact for fact in payload['facts'] if fact['id'] == 'F3')['changeMetrics']['scrapDelta'], {'available': False, 'value': None})
self.assertIs(next(fact for fact in payload['facts'] if fact['id'] == 'F1')['overallRateContribution']['available'], False)
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': '国内率下降0.62个百分点、报废增量5572.52 kg,海外率下降0.09个百分点、报废增量738.91 kg,是本轮下降的主要分组来源。规格乙报废增量仅个位数kg,规模上不足以解释总体变化。', 'factIds': ['F1', 'F2', 'F3']}]})
with patch('nao_native_answer.build_native_report', return_value=report): final = invoke(provider)
self.assertIn('国内率下降0.62个百分点、报废增量5572.52 kg,海外率下降0.09个百分点、报废增量738.91 kg。', final['answer'])
self.assertNotIn('主要分组来源', final['answer']); self.assertNotIn('个位数kg', final['answer'])
self.assertEqual(final['usedEvidenceRefs'], ['Q00000001'])
self.assertTrue(final['compositionDiagnostics']['sourceClauseRemovals'])
self.assertIsNone(_source_relation_gap({'text': '规格乙本期报废5.36 kg,报废增量未知,不能据此计算两期变化。', 'factIds': ['F3']}, facts))
def test_review_removes_only_exact_unsupported_clause_with_supported_remainder(self):
facts = [{'id': 'F1', 'kind': 'product', 'label': '规格乙', 'text': '规格乙黑片报废291.08→692.07 kg,亮片报废409.68→766.25 kg。', 'evidenceRef': 'Q00000001'}]
text = '规格乙黑片报废291.08→692.07 kg,亮片报废409.68→766.25 kg,亮片报废增幅更明显。'
report = {'facts': facts, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
for overrides, trimmed in (({}, True), ({'unsupportedClauses': ['并不存在的错误分句。']}, False),
({'unsupportedClauses': ['更明显']}, False), ({'remainingClaimsSupported': False}, False),
({'remainingStatisticalClaimsSupported': False}, False), ({'unsupportedClauses': []}, False)):
phases = []
def provider(request, timeout):
phases.append(request_phase(request))
if phases[-1] == 'evidence_review':
return response({'reviews': [{'supported': False, 'statisticalClaimsSupported': False,
'unsupportedClauses': ['亮片报废增幅更明显。'], 'remainingClaimsSupported': True,
'remainingStatisticalClaimsSupported': True, **overrides}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': text, 'factIds': ['F1']}]})
with self.subTest(overrides=overrides), patch('nao_native_answer.build_native_report', return_value=report):
final = invoke(provider, context={'_autonomousAnswer': True})
self.assertEqual(bool(final['compositionDiagnostics'].get('reviewClauseRemovals')), trimmed)
self.assertNotIn('亮片报废增幅更明显', final['answer'])
if trimmed:
self.assertIn('规格乙黑片报废291.08→692.07 kg,亮片报废409.68→766.25 kg。', final['readingSummary']['lead'])
self.assertNotIn('部分统计比较未通过核验', final['answer'])
self.assertEqual(phases, ['native_synthesis', 'evidence_review'])
def test_review_remainder_still_passes_numeric_and_production_guards(self):
facts = [{'id': 'F1', 'text': '合格率95.00%。', 'evidenceRef': 'Q00000001'}]
row = {'unsupportedClauses': ['错误比较。'], 'remainingClaimsSupported': True, 'remainingStatisticalClaimsSupported': True}
self.assertIsNone(_review_supported_remainder({'text': '合格率99.00%。错误比较。', 'factIds': ['F1']}, row, facts))
report = {'facts': facts, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'supported': False, 'statisticalClaimsSupported': False, **row}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': '合格率95.00%。错误比较。更接近爬坡损失。现场原因仍待验证。', 'factIds': ['F1']}]})
with patch('nao_native_answer.build_native_report', return_value=report): final = invoke(provider)
self.assertTrue(final['compositionDiagnostics']['reviewClauseRemovals'])
self.assertNotIn('更接近爬坡损失', final['answer'])
self.assertEqual(final['compositionDiagnostics']['hypothesisBlockIndexes'], [0])
def test_review_http_error_keeps_partial_and_only_records_status(self):
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
raise HTTPError('https://offline.invalid', 429, 'private-provider-message', {}, io.BytesIO(b'private-response-body'))
return response({'blocks': [{'role': 'core', 'text': '综合合格率95.00%。', 'factIds': ['F1']}]})
final = invoke(provider, context={'_autonomousAnswer': True})
self.assertFalse(final['evidenceSufficient'])
self.assertEqual(final['compositionDiagnostics']['reviewHttpStatus'], 429)
self.assertNotIn('private-', json.dumps(final, ensure_ascii=False))
def test_actual_product_dimension_defect_rows_keep_exact_cross_identity(self):
from test_nao_query_plan_defects_report_visuals import defect_result, document, RELEASE
result = defect_result(dimension='product')
report = build_native_report([document(result)], context={'releaseId': RELEASE,
'_agentTask': {'executionMode': 'native_agent'}}, question='核查规格具体缺陷')
self.assertFalse(report['missingEvidence'])
fact = next(fact for fact in report['facts'] if fact['kind'] == 'defect')
self.assertEqual(fact['scope']['filters'], {})
self.assertEqual(fact['verifiedDefectAssociation'], {'product': '规格A', 'defect': '责废尺寸重量', 'stage': 'black'})
product = {'id': 'P1', 'kind': 'product', 'label': '规格A', 'text': '规格A合格率95.00%。', 'evidenceRef': 'Q00000001'}
for phrase, valid in (('规格A的尺寸缺陷已有来源记录。', True), ('规格A的缺角类缺陷已有来源记录。', False)):
self.assertEqual(_source_relation_gap({'text': phrase, 'factIds': [product['id'], fact['id']]}, [product, fact]) is None, valid)
def provider(request, timeout):
payload = json.loads(json.loads(request.data)['messages'][1]['content'])
self.assertEqual(next(item for item in payload['facts'] if item['id'] == fact['id'])['verifiedDefectAssociation']['defect'], '责废尺寸重量')
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': '规格A的尺寸缺陷已有来源记录。', 'factIds': [fact['id']]}]})
with patch('nao_native_answer.build_native_report', return_value=report): final = invoke(provider)
self.assertTrue(final['evidenceSufficient'], final['compositionDiagnostics'])
def test_marginal_lists_cannot_prove_spec_defect_or_group_contribution(self):
product = '16.33X15.5X3.75+T'
facts = [{'id': 'F1', 'kind': 'product', 'label': product, 'text': product+'合格率95.00%。',
'evidenceRef': 'Q00000001', 'scope': {'filters': {'department': '新能源汽车磁钢'}}},
{'id': 'F2', 'kind': 'defect', 'text': '总体料废缺角重量10.00 kg。', 'evidenceRef': 'Q00000002',
'scope': {'filters': {'department': '新能源汽车磁钢'}}},
{'id': 'F3', 'kind': 'group', 'label': '汽车交通-海外', 'text': '汽车交通-海外合格率下降0.09个百分点。',
'evidenceRef': 'Q00000003'}]
for text, ids in ((product+'的缺角类缺陷已关联。现场根因仍待验证。', ['F1', 'F2']),
('汽车交通-海外下降0.09个百分点,影响很小。', ['F3'])):
self.assertIsNotNone(_source_relation_gap({'text': text, 'factIds': ids}, facts))
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': text, 'factIds': ids}]})
report = {'facts': facts, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
with patch('nao_native_answer.build_native_report', return_value=report): result = invoke(provider)
self.assertNotIn('影响很小', result['answer'])
self.assertNotIn('缺角类缺陷已关联', result['answer'])
self.assertTrue(result['compositionDiagnostics'].get('sourceRelationGaps') or result['compositionDiagnostics'].get('sourceClauseRemovals'))
pending = {'text': '重点核查'+product+'是否涉及总体缺角线索,尚未建立关联。', 'factIds': ['F1', 'F2']}
self.assertIsNone(_source_relation_gap(pending, facts))
associated = {'text': product+'的缺角类缺陷是本次核查线索。', 'factIds': ['F1', 'F2']}
for basis, kind in (('quality_fine_defects', 'defect'), ('protocol_history_snapshot', 'history')):
positive = copy.deepcopy(facts); positive[1].update(kind=kind, scope={'filters': {'product': product}}, validity={'basis': basis})
self.assertIsNone(_source_relation_gap(associated, positive))
positive[1]['text'] = '该规格料废尺寸重量10.00 kg。'
self.assertIsNotNone(_source_relation_gap(associated, positive))
def test_process_classification_is_unverified_but_denominator_explanation_survives(self):
for text in ('属于极小批量或停投产切换的比率噪声。', '更接近上量过程中的爬坡损失。',
'是换产造成的。', '由于停产导致率变化。'):
self.assertTrue(_affirmative_production_cause(text), text)
for text in ('是否涉及停投产切换需要现场核实。', '不能据此判断属于爬坡损失。',
'可能属于换产损失,尚待验证。', '停产尚未确认。',
'报废绝对量基本持平,投入基数下降使报废率上升。', '这反映范围切换,无法判断生产原因。'):
self.assertFalse(_affirmative_production_cause(text), text)
for phrase in ('属于极小批量或停投产切换的比率噪声。', '更接近上量过程中的爬坡损失。',
'属于极小批量或停投产切换的比率噪声。现场原因仍待验证。',
'更接近上量过程中的爬坡损失。现场原因仍待验证。'):
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True},
{'supported': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': '本期综合合格率95.00%,基期96.00%。', 'factIds': ['F1']},
{'role': 'key_evidence', 'text': phrase, 'factIds': ['F1']}]})
result = invoke(provider, context={'_autonomousAnswer': True})
self.assertIn('本期综合合格率95.00%,基期96.00%。', result['answer'])
self.assertNotIn(phrase, result['answer'])
self.assertEqual(result['compositionDiagnostics']['hypothesisBlockIndexes'], [1])
def test_prompt_keeps_business_core_and_requires_actual_spec_defect_cross_scope(self):
phases = []
def provider(request, timeout):
system = json.loads(request.data)['messages'][0]['content']
phase = request_phase(request); phases.append(phase)
if phase == 'evidence_review':
self.assertIn('真实facts的product筛选/交叉维度', system)
self.assertIn('没有正式总体贡献字段', system)
self.assertIn('数学分子分母关系与现场过程判断须分开', system)
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
self.assertIn('来源行数、present/missing/invalid/negative列表', system)
self.assertIn('不能作为core/key_evidence堆入首屏', system)
self.assertIn('不要直接写null、DIFF、INCOMPLETE', system)
self.assertIn('不能称已有统计关联', system)
self.assertIn('core/key_evidence合计须包含具体缺陷线索或明确的缺陷证据缺口', system)
return response({'blocks': [{'role': 'core', 'text': '本期综合合格率95.00%。', 'factIds': ['F1']}]})
result = invoke(provider, context={'_autonomousAnswer': True})
self.assertTrue(result['evidenceSufficient'], result['compositionDiagnostics'])
self.assertEqual(phases, ['native_synthesis', 'evidence_review'])
def test_fact_ids_derive_refs_and_unknown_sources_remain_rejected(self):
facts = [{'id': 'F1', 'text': '已核验事实甲。', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'text': '已核验事实乙。', 'evidenceRef': 'Q00000002'}]
for provided in (None, [], ['Q00000002'], ['Q00000001', 'Q00000002']):
block = {'text': '{{F1}}', 'factIds': ['F1']}
if provided is not None: block['evidenceRefs'] = provided
diagnostics = {}
blocks, _ = _validate_blocks({'blocks': [block]}, facts, _binding_inventory(facts), diagnostics=diagnostics)
self.assertEqual(blocks[0]['evidenceRefs'], ['Q00000001'])
self.assertEqual(bool(diagnostics.get('referenceNormalizations')), provided not in (None, ['Q00000001']))
for extra in ({'evidenceRefs': ['Q99999999']}, {'evidenceRefs': [1]}, {'text': '{{F1}}[Q99999999]'}):
with self.subTest(extra=extra), self.assertRaises(NativeCompositionError):
_validate_blocks({'blocks': [{'text': '{{F1}}', 'factIds': ['F1'], **extra}]}, facts, _binding_inventory(facts))
def test_actual_platform_visuals_reach_both_calls_without_series_or_mutation(self):
for state in ('ready', 'unavailable'):
visual = {'visualState': state, 'charts': [{'id': 'line1', 'type': 'line', 'metric': 'rate',
'title': '月度合格率', 'period': {'year': 2026, 'monthStart': 8, 'monthEnd': 9},
'comparisonPeriod': None, 'scope': {'department': '新能源汽车磁钢'},
'evidenceRefs': ['Q00000001'], 'pointCount': 2, 'series': [{'value': 95}]}],
'missingEvidence': [] if state == 'ready' else ['正式图表暂不可用。']}
original = copy.deepcopy(visual); payloads = []
def provider(request, timeout):
payloads.append(json.loads(json.loads(request.data)['messages'][1]['content']))
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': '本期综合合格率95.00%。', 'factIds': ['F1']}]})
result = invoke(provider, context={'_autonomousAnswer': True, '_platformVisuals': visual})
self.assertTrue(result['compositionDiagnostics']['evidenceReviewAttempted'])
self.assertEqual(payloads[0]['platformVisuals'], payloads[1]['platformVisuals'])
self.assertEqual(payloads[0]['platformVisuals']['visualState'], state)
self.assertEqual(payloads[0]['platformVisuals']['charts'][0]['scope'], visual['charts'][0]['scope'])
self.assertNotIn('series', payloads[0]['platformVisuals']['charts'][0])
self.assertEqual(visual, original)
def test_same_response_object_and_shared_unit_recover_exact_missing_bindings(self):
a, b = '16.33X15.5X3.75+T', '17.85X16.9X6.5+T'
scope = {'filters': {'department': '新能源汽车磁钢'}, 'period': {'year': 2026, 'month': 9}, 'dimension': 'product'}
facts = [{'id': 'F1', 'kind': 'product', 'label': a, 'text': a+'合格率0.00%。', 'evidenceRef': 'Q00000001', 'scope': scope},
{'id': 'F2', 'kind': 'supporting', 'label': a+'规模依据', 'text': a+'黑片投入0.00→10.44 kg。', 'evidenceRef': 'Q00000001', 'scope': scope},
{'id': 'F3', 'kind': 'product', 'label': b, 'text': b+'合格率0.00%。', 'evidenceRef': 'Q00000001', 'scope': scope},
{'id': 'F4', 'kind': 'supporting', 'label': b+'规模依据', 'text': b+'黑片投入2.52 kg。', 'evidenceRef': 'Q00000001', 'scope': scope}]
for same_frame in (True, False):
candidate = copy.deepcopy(facts)
for fact in candidate: fact['scope'] = copy.deepcopy(fact['scope'])
if not same_frame:
for fact in candidate[:2]: fact['scope']['filters'] = {'department': '另一部门'}
report = {'facts': candidate, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
payload = fixture_payload(request)
self.assertEqual(set(payload['blocks'][0]['factIds']), {'F1', 'F2', 'F3', 'F4'})
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': a+'黑片投入0.00→10.44 kg;'+b+'黑片投入2.52 kg。', 'factIds': ['F3', 'F4']}]})
with self.subTest(same_frame=same_frame), patch('nao_native_answer.build_native_report', return_value=report):
result = invoke(provider)
self.assertEqual(result['evidenceSufficient'], same_frame, result['compositionDiagnostics'])
if same_frame:
self.assertFalse(result['compositionRepairAttempted'])
self.assertTrue(result['compositionDiagnostics']['identifierFactCompletions'])
def test_calendar_values_are_shared_across_product_frames_and_adjacent_measurements(self):
facts = []
for index, (product, month, weight) in enumerate((('规格甲', 9, '317.0621'), ('规格乙', 8, '162.7278'), ('规格丙', 9, '25.00')), 1):
facts.append({'id': 'F'+str(index), 'kind': 'product', 'label': product,
'text': product+f'2026年{month}月来源重量'+weight+'。', 'evidenceRef': f'Q{index:08d}',
'scope': {'period': {'year': 2026, 'month': month}, 'filters': {'product': product}},
'verifiedDisplayValues': [{'metricCode': 'weight', 'unit': 'source_weight', 'displayValue': weight}]})
report = {'facts': facts, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': '来源重量:规格甲9月317.0621,规格乙8月162.7278;规格丙同月另列。【1】', 'factIds': ['F1', 'F2', 'F3']}]})
with patch('nao_native_answer.build_native_report', return_value=report):
result = invoke(provider)
self.assertTrue(result['evidenceSufficient'], result['compositionDiagnostics'])
self.assertFalse(result['compositionRepairAttempted'])
self.assertNotIn('【1】', result['answer'])
self.assertIn('9月 317.0621', result['answer'])
def test_authoritative_alias_is_scoped_and_negative_cause_boundary_is_preserved(self):
for department in ('新能源汽车磁钢', '另一部门'):
fact = {'id': 'F1', 'kind': 'overall', 'text': '综合合格率95.00%。', 'evidenceRef': 'Q00000001', 'scope': {'filters': {'department': department}}}
report = {'facts': [fact], 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
def provider(request, timeout):
payload = json.loads(json.loads(request.data)['messages'][1]['content'])
aliases = payload['authoritativeScopeAliases']
self.assertEqual(bool(aliases), department == '新能源汽车磁钢')
if aliases:
self.assertEqual(aliases[0]['alias'], '汽车交通')
self.assertTrue(aliases[0]['preserveOtherFilters'])
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'supported': department == '新能源汽车磁钢', 'statisticalClaimsSupported': True}],
'missingGoalDescriptions': [] if aliases else ['汽车交通范围未匹配。']})
self.assertNotIn('numericTokens', payload)
return response({'blocks': [{'role': 'core', 'text': '汽车交通综合合格率95.00%,不能单独确认现场根因,不能据此断言具体生产根因。', 'factIds': ['F1']}]})
with self.subTest(department=department), patch('nao_native_answer.build_native_report', return_value=report):
result = invoke(provider, context={'_autonomousAnswer': True})
self.assertEqual(result['evidenceSufficient'], department == '新能源汽车磁钢')
if department == '新能源汽车磁钢':
self.assertIn('不能单独确认现场根因', result['answer'])
self.assertNotIn('待验证假设', result['answer'])
def test_partial_legacy_review_preserves_valid_core_but_never_guesses_bad_key(self):
for role in ('detail', 'key_evidence'):
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'index': 0, 'supported': True, 'statisticalClaimsSupported': True},
{'index1,': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': '本期综合合格率95.00%,基期96.00%。', 'factIds': ['F1']},
{'role': role, 'text': '所有质量风险已经完全排除。', 'factIds': ['F1']}]})
with self.subTest(role=role):
result = invoke(provider, context={'_autonomousAnswer': True})
self.assertEqual(result['evidenceSufficient'], role == 'detail')
self.assertIn('本期综合合格率95.00%,基期96.00%。', result['readingSummary']['lead'])
self.assertNotIn('风险已经完全排除', result['answer'])
self.assertEqual(result['compositionDiagnostics']['unreviewedBlockIndexes'], [1])
self.assertEqual(result['compositionDiagnostics']['coreReviewComplete'], role == 'detail')
def test_ordered_review_requires_exact_length_and_preserves_explicit_core_collection(self):
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
self.assertNotIn('"index":实际', json.loads(request.data)['messages'][0]['content'])
payload = fixture_payload(request)
self.assertEqual([block['index'] for block in payload['summaryBlocks']], [0, 2, 1])
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True} for _ in range(3)], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': '本期综合合格率95.00%。', 'factIds': ['F1']},
{'role': 'key_evidence', 'text': '正式下降1.00个百分点。', 'factIds': ['F1']},
{'role': 'core', 'text': '基期综合合格率96.00%。', 'factIds': ['F1']}]})
result = invoke(provider, context={'_autonomousAnswer': True})
self.assertTrue(result['evidenceSufficient'], result['compositionDiagnostics'])
self.assertEqual(result['compositionDiagnostics']['reviewSchema'], 'ordered')
self.assertEqual(len(result['readingSummary']['evidence']), 2)
self.assertIn('96.00%', result['readingSummary']['evidence'][0])
def test_formal_bare_metadata_counts_and_differences_bind_without_new_units(self):
facts = [{'id': 'F1', 'text': '来源重量290.8731;来源行 present/missing/invalid/negative = 532/0/0/0。', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'text': '分类核对状态DIFF,差值 = 0.2496。', 'evidenceRef': 'Q00000001'}]
report = {'facts': facts, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
for text, supported in (('有效行532,missing 0、invalid 0、negative 0,分类差值0.2496。', True),
('有效行532 kg。', False), ('分类差值0.5。', False)):
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': text, 'factIds': ['F1', 'F2']}]})
with self.subTest(text=text), patch('nao_native_answer.build_native_report', return_value=report):
result = invoke(provider)
self.assertEqual(result['evidenceSufficient'], supported, result['compositionDiagnostics'])
self.assertEqual(result['compositionRepairAttempted'], not supported)
def test_signed_metadata_after_bound_month_preserves_exact_sign_and_source(self):
facts = [{'id': 'F1', 'text': '2026年9月;分类核对状态DIFF,差值 = -1.5939。', 'evidenceRef': 'Q00000001',
'scope': {'period': {'year': 2026, 'month': 9}}},
{'id': 'F2', 'text': '2026年8月;分类核对状态DIFF,差值 = -0.2976。', 'evidenceRef': 'Q00000002',
'scope': {'period': {'year': 2026, 'month': 8}}}]
tokens = _binding_inventory(facts)
for text, ids, supported in (('9月-1.5939、8月-0.2976。', ['F1', 'F2'], True),
('9月+1.5939。', ['F1'], False),
('9月-1.5938。', ['F1'], False),
('9月-1.5939 kg。', ['F1'], False),
('9月-0.2976。', ['F1'], False)):
with self.subTest(text=text):
diagnostics = {}
parsed = _bind_exact_periods({'blocks': [{'text': text, 'factIds': ids}]}, tokens, diagnostics)
parsed = _bind_literal_measures(parsed, tokens, diagnostics)
if supported:
blocks, _ = _validate_blocks(parsed, facts, tokens)
self.assertEqual(len(diagnostics['literalBindings']), 2)
self.assertIn('-1.5939', blocks[0]['text'])
self.assertIn('-0.2976', blocks[0]['text'])
else:
with self.assertRaises(NativeCompositionError):
_validate_blocks(parsed, facts, tokens)
def test_numeric_hypothesis_without_comparison_keyword_still_needs_statistical_verdict(self):
facts = [{'id': 'F1', 'text': '规格甲报废重量0.59 kg,规格乙报废重量0.34 kg。', 'evidenceRef': 'Q00000001'}]
report = {'facts': facts, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
for phrase in ('均未达到0.59 kg', '均未达0.59 kg', '相等'):
for verdict in (None, False):
phases = []
def provider(request, timeout):
phases.append(request_phase(request))
if phases[-1] == 'evidence_review':
self.assertTrue(fixture_payload(request)['blocks'][0]['statisticalClaimsReviewRequired'])
row = {'supported': True}
if verdict is not None: row['statisticalClaimsSupported'] = verdict
return response({'reviews': [row], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': '0.59 kg和0.34 kg'+phrase+',现场原因待验证。',
'factIds': ['F1'], 'claimType': 'hypothesis'}]})
with self.subTest(phrase=phrase, verdict=verdict), patch('nao_native_answer.build_native_report', return_value=report):
result = invoke(provider)
self.assertNotIn(phrase, result['answer'])
diagnostics = result['compositionDiagnostics']
if phrase == '均未达0.59 kg':
# The recognized strict boundary is now rejected
# before semantic review: 0.59 is not below itself.
self.assertFalse(result['evidenceSufficient'])
self.assertFalse(result['bodyEvidenceSufficient'])
self.assertEqual(phases, ['native_synthesis', 'native_synthesis'])
self.assertTrue(diagnostics['formatRepairAttempted'])
self.assertFalse(diagnostics['evidenceReviewAttempted'])
self.assertNotIn('rejectedStatisticalBlockIndexes', diagnostics)
self.assertEqual(len(diagnostics['issues']), 2)
self.assertTrue(all(issue.startswith('NativeCompositionError:') and '0.59' in issue
for issue in diagnostics['issues']))
else:
self.assertEqual(phases, ['native_synthesis', 'evidence_review'])
self.assertTrue(diagnostics['evidenceReviewAttempted'])
self.assertEqual(diagnostics['rejectedStatisticalBlockIndexes'], [0])
def test_strict_comparison_threshold_is_not_replaced_by_rounded_source_value(self):
for source_unit, suffix in (('kg', ' kg'), ('source_weight', '')):
facts = [{'id': 'F1', 'text': '报废重量0.59'+suffix+',另一规格报废重量0.34'+suffix+'。', 'evidenceRef': 'Q00000001',
**({'verifiedDisplayValues': [{'metricCode': 'weight', 'unit': 'source_weight', 'displayValue': '0.59'},
{'metricCode': 'weight', 'unit': 'source_weight', 'displayValue': '0.34'}]} if source_unit == 'source_weight' else {})}]
tokens = _binding_inventory(facts)
for text in ('两项报废均不足1'+suffix+'。', '报废高于1'+suffix+'。'):
with self.subTest(source_unit=source_unit, text=text), self.assertRaises(NativeCompositionError):
_validate_blocks(_bind_literal_measures({'blocks': [{'text': text, 'factIds': ['F1']}]}, tokens, {}), facts, tokens)
def test_first_repair_reports_selection_and_body_issues_together_then_accepts_explicit_roles(self):
calls = []
def provider(request, timeout):
calls.append(request_phase(request))
if calls[-1] == 'evidence_review':
self.assertEqual(fixture_payload(request)['summarySelection']['selectionMode'], 'block_roles')
return response({'reviews': [{'index': 0, 'supported': True}, {'index': 1, 'supported': True}], 'missingGoalDescriptions': []})
if len(calls) == 1:
return response({'blocks': [{'id': 'core', 'text': '本期变化{{F1}}。', 'factIds': ['F1']},
{'id': 'detail', 'text': '应结合阶段核查。', 'factIds': ['F1']}],
'summarySelection': {'leadId': 'core', 'evidenceIds': ['Q00000001']}})
feedback = json.loads(request.data)['messages'][-1]['content']
self.assertIn('Q证据编号', feedback)
self.assertIn('不得把F', feedback)
return response({'blocks': [{'role': 'core', 'text': '本期合格率95.00%,基期96.00%。', 'factIds': ['F1']},
{'role': 'key_evidence', 'text': '正式下降1.00个百分点。', 'factIds': ['F1']}]})
result = invoke(provider)
self.assertEqual(calls, ['native_synthesis', 'native_synthesis', 'evidence_review'])
self.assertTrue(result['evidenceSufficient'], result['compositionDiagnostics'])
self.assertEqual(len(result['readingSummary']['evidence']), 1)
def test_hypothesis_label_cannot_preserve_unverified_numeric_comparison(self):
facts = [{'id': 'F1', 'text': '规格甲报废重量2.52 kg。', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'text': '规格乙报废重量0.59 kg。', 'evidenceRef': 'Q00000001'},
{'id': 'F3', 'text': '规格丙报废重量0.34 kg。', 'evidenceRef': 'Q00000001'}]
report = {'facts': facts, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
for claim_type, statistical_support in (('hypothesis', None), ('hypothesis', False), ('observation', False)):
phases = []
def provider(request, timeout):
phases.append(request_phase(request))
if phases[-1] == 'evidence_review':
payload = fixture_payload(request)
self.assertEqual(payload['blocks'][0]['statisticalClaimsReviewRequired'], claim_type == 'hypothesis')
row = {'index': 0, 'supported': True}
if statistical_support is not None: row['statisticalClaimsSupported'] = statistical_support
return response({'reviews': [row], 'missingGoalDescriptions': []})
return response({'blocks': [{'role': 'core', 'text': '报废2.52 kg为其中较高者,另两项0.59 kg和0.34 kg均不足0.59 kg,现场原因仍待验证。',
'factIds': ['F1', 'F2', 'F3'], 'claimType': claim_type}]})
with self.subTest(claim_type=claim_type, statistical_support=statistical_support), patch('nao_native_answer.build_native_report', return_value=report):
result = invoke(provider)
self.assertNotIn('其中较高', result['answer'])
self.assertNotIn('均不足', result['answer'])
self.assertIn('报废重量2.52 kg', result['answer'])
# Keep the original false comparison. Its strict threshold
# fails the deterministic gate, so no semantic verdict exists.
diagnostics = result['compositionDiagnostics']
self.assertFalse(result['evidenceSufficient'])
self.assertFalse(result['bodyEvidenceSufficient'])
self.assertEqual(phases, ['native_synthesis', 'native_synthesis'])
self.assertTrue(diagnostics['formatRepairAttempted'])
self.assertFalse(diagnostics['evidenceReviewAttempted'])
self.assertNotIn('rejectedStatisticalBlockIndexes', diagnostics)
self.assertEqual(len(diagnostics['issues']), 2)
self.assertTrue(all(issue.startswith('NativeCompositionError:') and '0.59' in issue
for issue in diagnostics['issues']))
def test_history_source_overall_and_stage_baselines_are_protected_before_exploratory_rows(self):
scope = {'filters': {'department': '原调查部门'}, 'period': {'year': 2026, 'month': 9}}
noise = [{'id': 'F'+str(index), 'text': '探索规格'+str(index)+'合格率0.00%。', 'kind': 'history',
'evidenceRef': 'history:'+str(index), 'scope': {**scope, 'dimension': 'product'}} for index in range(1, 100)]
baseline = [{'id': 'F'+str(100+index), 'kind': kind, 'label': label, 'text': label+'合格率95.00%。',
'evidenceRef': 'history:baseline'+str(index), 'scope': scope}
for index, (kind, label) in enumerate((('overall', '综合'), ('stage', '黑片'), ('stage', '亮片')))]
report = {'facts': noise+baseline, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
selected, _, _ = _select_fact_pool(report, '汇总原调查报告', {'_agentTask': {'kind': 'report'}, '_historySourceGoals': [{'filters': scope['filters']}]})
self.assertEqual([fact['id'] for fact in selected['facts'][:3]], ['F100', 'F101', 'F102'])
def test_verified_source_weight_binds_without_inventing_physical_unit(self):
facts = [{'id': 'F1', 'text': '来源重量10354.0306。', 'evidenceRef': 'history:weight',
'verifiedDisplayValues': [{'metricCode': 'weight', 'unit': 'source_weight', 'displayValue': '10354.0306'}]}]
tokens = _binding_inventory(facts)
parsed = _bind_literal_measures({'blocks': [{'text': '来源重量10,354.03,应核查其变化。', 'factIds': ['F1']}]}, tokens, {})
self.assertIn('10354.0306', _validate_blocks(parsed, facts, tokens)[0][0]['text'])
for text in ('来源重量10354.0306 kg。', '来源重量99999。'):
with self.subTest(text=text), self.assertRaises(NativeCompositionError):
_validate_blocks(_bind_literal_measures({'blocks': [{'text': text, 'factIds': ['F1']}]}, tokens, {}), facts, tokens)
key = next(key for key, value in tokens.items() if value.get('unit') == 'source_weight')
with self.assertRaisesRegex(NativeCompositionError, '单位'):
_validate_blocks({'blocks': [{'text': '{{'+key+'}} kg', 'factIds': ['F1']}]}, facts, tokens)
def test_source_weight_rounding_uses_bounded_precision_and_skips_unsafe_literals(self):
official = '123456789012345678901234567890.12345'
facts = [{'id': 'F1', 'text': '来源重量' + official + '。', 'evidenceRef': 'history:weight',
'verifiedDisplayValues': [{'metricCode': 'weight', 'unit': 'source_weight', 'displayValue': official}]}]
tokens = _binding_inventory(facts)
diagnostics = {}
accepted = _bind_literal_measures({'blocks': [{'text': '来源重量123456789012345678901234567890.1235。',
'factIds': ['F1']}]}, tokens, diagnostics)
self.assertIn('{{N', accepted['blocks'][0]['text'])
self.assertEqual(len(diagnostics['literalBindings']), 1)
rejected_diagnostics = {}
rejected = _bind_literal_measures({'blocks': [{'text': '来源重量123456789012345678901234567890.1236。',
'factIds': ['F1']}]}, tokens, rejected_diagnostics)
self.assertIn('123456789012345678901234567890.1236', rejected['blocks'][0]['text'])
self.assertFalse(rejected_diagnostics.get('literalBindings'))
oversized = '7.' + ('0' * 300) + '1'
oversized_facts = [{'id': 'F2', 'text': '来源重量' + oversized + '。', 'evidenceRef': 'history:oversized',
'verifiedDisplayValues': [{'metricCode': 'weight', 'unit': 'source_weight',
'displayValue': oversized}]}]
oversized_tokens = _binding_inventory(oversized_facts)
oversized_diagnostics = {}
oversized_result = _bind_literal_measures({'blocks': [{'text': '来源重量' + oversized + '。',
'factIds': ['F2']}]},
oversized_tokens, oversized_diagnostics)
self.assertIn(oversized, oversized_result['blocks'][0]['text'])
self.assertFalse(oversized_diagnostics.get('literalBindings'))
def test_summary_selection_rejects_unknown_and_nonpriority_core_and_normalizes_redundancy(self):
blocks = [{'text': '最低率规格甲', 'bindingFactIds': ['F1']}, {'text': '高报废规格乙', 'bindingFactIds': ['F2']}]
for choice in ({'leadIndex': True, 'evidenceIndexes': []}, {'leadIndex': 0, 'evidenceIndexes': [2]},
{'leadIndex': 1, 'evidenceIndexes': []}):
with self.subTest(choice=choice), self.assertRaises(NativeCompositionError):
_validate_summary_selection({'summarySelection': choice}, blocks, ['F1'])
self.assertEqual(_validate_summary_selection({'summarySelection': {'leadIndex': 0, 'evidenceIndexes': [1, 0, 1]}},
blocks, ['F1']), {'leadIndex': 0, 'evidenceIndexes': [1]})
named = [{**block, 'id': name} for block, name in zip(blocks, ('core', 'details'))]
self.assertEqual(_validate_summary_selection({'summarySelection': {'leadId': 'core', 'evidenceIds': ['details', 'core', 'details']}}, named),
{'leadIndex': 0, 'evidenceIndexes': [1], 'leadId': 'core', 'evidenceIds': ['details']})
for choice in ({'leadId': 'unknown', 'evidenceIds': []}, {'leadId': 'core', 'evidenceIds': ['F2']},
{'leadId': 'core', 'evidenceIds': [1]}):
with self.subTest(choice=choice), self.assertRaises(NativeCompositionError):
_validate_summary_selection({'summarySelection': choice}, named, single_query=True)
with self.assertRaises(NativeCompositionError):
_validate_summary_selection({'summarySelection': {'leadId': 'core', 'evidenceIds': []}}, blocks)
def test_explicit_summary_selection_can_show_key_product_from_later_body(self):
facts = [{'id': 'F1', 'kind': 'overall', 'label': '总体', 'text': '总体合格率95.00%。', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'kind': 'product', 'label': '重点规格乙', 'text': '重点规格乙黑片报废重量10.00 kg。', 'evidenceRef': 'Q00000002'}]
report = {'facts': facts, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
blocks = [{'text': '总体合格率95.00%,应继续核查变化关系。', 'factIds': ['F1']}]
blocks += [{'text': text, 'factIds': ['F1']} for text in ('总体期间适用本轮范围。', '应分别看率值和规模。', '阶段表现需要结合投入。', '不同对象不可混合。', '正式值沿用原口径。')]
blocks += [{'text': '重点规格乙黑片报废重量10.00 kg,是需要核查的规格线索。', 'factIds': ['F2']}]
for index, block in enumerate(blocks):
block['id'] = 'core' if index == 0 else 'focus_specs' if index == 6 else 'detail_' + str(index)
block['role'] = 'core' if index == 0 else 'key_evidence' if index == 6 else 'detail'
calls = []
def provider(request, timeout):
calls.append(request_phase(request))
if calls[-1] == 'evidence_review':
payload = fixture_payload(request)
self.assertEqual(payload['summarySelection'], {'leadIndex': 0, 'evidenceIndexes': [6], 'selectionMode': 'block_roles'})
self.assertEqual([item['index'] for item in payload['summaryBlocks']], [0, 6])
return response({'reviews': [{'index': index, 'supported': True} for index in range(7)], 'missingGoalDescriptions': []})
return response({'blocks': blocks})
with patch('nao_native_answer.build_native_report', return_value=report):
result = invoke(provider, context={'_autonomousAnswer': True})
self.assertEqual(calls, ['native_synthesis', 'evidence_review'])
self.assertTrue(result['evidenceSufficient'])
self.assertEqual(len(result['readingSummary']['evidence']), 1)
self.assertIn('重点规格乙', result['readingSummary']['evidence'][0])
self.assertNotIn('正式值沿用原口径', result['readingSummary']['evidence'][0])
def test_simple_overall_metrics_requires_only_overall_comparison_and_keeps_one_short_block(self):
task = {'kind': 'metrics', 'fields': ['rate'], 'stages': [], 'dimensions': [], 'filters': {},
'period': {'year': 2026, 'month': 9}, 'comparisonPeriod': {'year': 2026, 'month': 8}, 'comparisonRequired': True}
context = {'_autonomousAnswer': True, '_agentTask': task}
report = build_native_report(evidence(), context, '只查本期综合合格率,取消排名和规模')
self.assertEqual(len(report['requiredFactIds']), 1)
self.assertEqual(next(fact for fact in report['facts'] if fact['id'] == report['requiredFactIds'][0])['kind'], 'overall')
self.assertTrue(any(fact['kind'] == 'stage' for fact in report['facts']))
single = build_native_report(evidence(), {'_agentTask': {**task, 'comparisonRequired': False}}, '只查本期综合合格率')
single_fact = next(fact for fact in single['facts'] if fact['id'] == single['requiredFactIds'][0])
self.assertEqual(single_fact['period'], task['period'])
self.assertNotIn('→', single_fact['text'])
calls = []
def provider(request, timeout):
calls.append(request_phase(request))
if calls[-1] == 'evidence_review':
self.assertEqual(len(fixture_payload(request)['blocks']), 1)
return response({'reviews': [{'index': 0, 'supported': True}], 'missingGoalDescriptions': []})
payload = fixture_payload(request)
self.assertEqual(payload['requiredFactIds'], ['F1'])
self.assertIn('只是同范围综合合格率简单查询', json.loads(request.data)['messages'][0]['content'])
return response({'blocks': [{'id': 'core', 'text': '本期综合合格率95.00%,较基期96.00%下降1.00个百分点。', 'factIds': ['F1']}],
'summarySelection': {'leadId': 'core', 'evidenceIds': []}})
result = invoke(provider, context=context)
self.assertTrue(result['evidenceSufficient'], result['compositionDiagnostics'])
self.assertEqual(calls, ['native_synthesis', 'evidence_review'])
self.assertEqual(result['readingSummary']['evidence'], [])
self.assertNotIn('阶段', result['answer'])
self.assertNotIn('根因', result['answer'])
def test_single_simple_query_uses_sole_bound_block_without_summary_metadata_failure(self):
docs = [{'evidenceRef': 'Q00000001', 'result': {'kind': 'quality_metrics', 'period': {'year': 2026, 'month': 9},
'filters': {}, 'summary': {'compositeRate': .9577},
'coverage': {'sourceMonths': [{'year': 2026, 'month': 9, 'blackRows': 2, 'brightRows': 2}]}}}]
context = {'_autonomousAnswer': True, '_agentTask': {'kind': 'metrics', 'fields': ['rate'], 'stages': [],
'dimensions': [], 'period': {'year': 2026, 'month': 9}, 'filters': {}, 'comparisonRequired': False}}
for selection in (None, {'leadId': 'core', 'evidenceIds': ['Q00000001']}):
calls = []
def provider(request, timeout):
calls.append(request_phase(request))
if calls[-1] == 'evidence_review':
self.assertEqual(fixture_payload(request)['summarySelection']['evidenceIds'], [])
return response({'reviews': [{'index': 0, 'supported': True}], 'missingGoalDescriptions': []})
value = {'blocks': [{'id': 'core', 'text': '本期综合合格率95.77%。', 'factIds': ['F1']}]}
if selection is not None: value['summarySelection'] = selection
return response(value, with_summary=False)
with self.subTest(selection=selection):
result = invoke(provider, docs=docs, context=context)
self.assertTrue(result['evidenceSufficient'], result['compositionDiagnostics'])
self.assertEqual(calls, ['native_synthesis', 'evidence_review'])
self.assertFalse(result['compositionRepairAttempted'])
self.assertIn('95.77%', result['readingSummary']['lead'])
self.assertTrue(result['compositionDiagnostics']['singleQuerySummaryNormalizations'])
def test_single_simple_query_still_rejects_unknown_numeric_value(self):
docs = [{'evidenceRef': 'Q00000001', 'result': {'kind': 'quality_metrics', 'period': {'year': 2026, 'month': 9},
'filters': {}, 'summary': {'compositeRate': .9577},
'coverage': {'sourceMonths': [{'year': 2026, 'month': 9, 'blackRows': 2, 'brightRows': 2}]}}}]
context = {'_autonomousAnswer': True, '_agentTask': {'kind': 'metrics', 'fields': ['rate'], 'stages': [],
'dimensions': [], 'period': {'year': 2026, 'month': 9}, 'filters': {}, 'comparisonRequired': False}}
result = invoke(lambda *args, **kwargs: response({'blocks': [{'id': 'core', 'text': '本期综合合格率99.77%。', 'factIds': ['F1']}]}, with_summary=False),
docs=docs, context=context)
self.assertFalse(result['evidenceSufficient'])
self.assertTrue(result['compositionRepairAttempted'])
self.assertFalse(result['compositionDiagnostics']['evidenceReviewAttempted'])
self.assertIsNone(result['readingSummary'])
def test_missing_summary_selection_has_one_repair_and_keeps_full_verified_body(self):
calls = []
def provider(request, timeout):
calls.append(request_phase(request))
if calls[-1] == 'evidence_review':
self.assertIsNone(fixture_payload(request)['summarySelection'])
return response({'reviews': [{'index': 0, 'supported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'text': '本期综合合格率95.00%,应结合阶段核查。', 'factIds': ['F1']}]}, with_summary=False)
result = invoke(provider, context={'_autonomousAnswer': True})
self.assertEqual(calls, ['native_synthesis', 'native_synthesis', 'evidence_review'])
self.assertFalse(result['evidenceSufficient'])
self.assertIn('本期综合合格率95.00%,应结合阶段核查。', result['answer'])
self.assertIsNone(result['readingSummary'])
self.assertIn('核心摘要选择格式未通过', result['answer'])
self.assertEqual(result['usedEvidenceRefs'], ['Q00000001'])
self.assertEqual(len(result['rawModelResponses']), 3)
def test_literal_measures_bind_precision_and_units_without_accepting_wrong_units_or_values(self):
facts = [{'id': 'F1', 'kind': 'stage', 'label': '黑片',
'text': '2026年9月黑片投入重量1,000.00 kg,报废重量10.00 kg;合格率95.13%,下降0.62个百分点。',
'evidenceRef': 'Q00000001', 'scope': {'period': {'year': 2026, 'month': 9}}}]
tokens = _binding_inventory(facts)
text = '2026年9月黑片合格率95.130%,下降0.620个百分点;投入1000公斤,报废10.0 kg。'
diagnostics = {}
parsed = _bind_literal_measures(_bind_exact_periods({'blocks': [{'text': text, 'factIds': ['F1']}]}, tokens, diagnostics), tokens, diagnostics)
blocks, _ = _validate_blocks(parsed, facts, tokens)
self.assertIn('95.13%', blocks[0]['text'])
self.assertIn('1,000.00公斤', blocks[0]['text'])
self.assertEqual(len(diagnostics['literalBindings']), 4)
for bad in ('合格率95.13 kg', '下降0.62%', '报废9999 kg', '投入1吨', '2025年9月合格率95.13%'):
with self.subTest(bad=bad), self.assertRaises(NativeCompositionError):
parsed = _bind_literal_measures(_bind_exact_periods({'blocks': [{'text': bad, 'factIds': ['F1']}]}, tokens, {}), tokens, {})
_validate_blocks(parsed, facts, tokens)
rounded = _bind_literal_measures({'blocks': [{'text': '黑片合格率95.1%。', 'factIds': ['F1']}]}, tokens, {})
self.assertIn('95.13%', _validate_blocks(rounded, facts, tokens)[0][0]['text'])
def test_mixed_fact_sentences_get_one_natural_repair_and_short_lead(self):
calls = []
def provider(request, timeout):
calls.append(request_phase(request))
if calls[-1] == 'evidence_review':
return response({'reviews': [{'index': 0, 'supported': True}], 'missingGoalDescriptions': []})
if len(calls) == 1:
return response({'blocks': [{'text': '对比{{F1}}显示本期下降,应继续核查。', 'factIds': ['F1']}]})
return response({'blocks': [{'text': '本期综合合格率95.00%,较基期96.00%下降1.00个百分点,需继续核查阶段影响。', 'factIds': ['F1']}],
'summarySelection': {'leadIndex': 0, 'evidenceIndexes': [0, 0]}})
result = invoke(provider)
self.assertEqual(calls, ['native_synthesis', 'native_synthesis', 'evidence_review'])
self.assertTrue(result['evidenceSufficient'], result['compositionDiagnostics'])
self.assertLess(len(result['readingSummary']['lead']), 120)
self.assertNotIn('综合合格率:2026', result['readingSummary']['lead'])
self.assertTrue(result['compositionDiagnostics']['literalBindings'])
self.assertEqual(result['compositionDiagnostics']['summarySelection'], {'leadIndex': 0, 'evidenceIndexes': []})
self.assertTrue(result['compositionDiagnostics']['summarySelectionNormalizations'])
def test_custom_or_unknown_formal_formula_is_not_replaced_by_product(self):
custom = {'supportsStageContribution': False,
'metricRules': {'compositeRate': {'formula': '(blackRate+brightRate)/2'}}}
result = _metric_semantics({'calculationRules': [custom]})
self.assertIs(result['productConfirmed'], False)
self.assertEqual(result['formalRules'][0]['metricRules']['compositeRate']['formula'], '(blackRate+brightRate)/2')
self.assertIsNone(_metric_semantics({})['productConfirmed'])
supported = _metric_semantics({}, {'calculationRules': {'supportsStageContribution': True,
'combined': 'blackRate * brightRate'}})
self.assertIs(supported['productConfirmed'], True)
def test_real_tool_formula_metadata_is_preserved_for_synthesis_and_review(self):
docs = evidence()
custom = {'supportsStageContribution': False,
'metricRules': {'compositeRate': {'formula': '(blackRate+brightRate)/2'}}}
descriptions = {'compositeRate': '自定义两阶段均值'}
for side in ('current', 'comparison'):
docs[0]['result'][side]['calculationRules'] = custom
docs[0]['result'][side]['metricFormulaDescriptions'] = descriptions
report = build_native_report(docs)
semantics = _metric_semantics(report)
self.assertIs(semantics['productConfirmed'], False)
self.assertIn(custom, semantics['formalRules'])
self.assertIn(descriptions, semantics['formalDescriptions'])
def test_layout_explanation_is_private_and_does_not_fill_business_notices(self):
facts = [{'id': 'F1', 'kind': 'overall', 'label': '总体', 'text': '总体合格率95.00%。', 'evidenceRef': 'Q00000001'}]
report = {'facts': facts, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [],
'rejectedEvidenceRefs': [], 'notices': ['正文选取关键事实解释;其余已核验结果保留在完整统计附录。']}
with patch('nao_native_answer.build_native_report', return_value=report):
result = invoke(lambda *args, **kwargs: response({'blocks': [{'text': '{{F1}}', 'factIds': ['F1']}]}))
self.assertFalse(result['readingSummary']['notices'])
def test_ordinary_structural_words_go_to_semantic_review_without_format_retry(self):
calls = []
def provider(request, timeout):
calls.append(request_phase(request))
if calls[-1] == 'evidence_review':
payload = fixture_payload(request)
self.assertIn('一个相反线索', payload['blocks'][0]['text'])
self.assertIn('两个方面', payload['blocks'][0]['text'])
return response({'reviews': [{'index': 0, 'supported': False}], 'missingGoalDescriptions': []})
return response({'blocks': [{'text': '一个相反线索涉及两个方面和2个类别,第1项可列前1-5,这两类方向都已得到确认。',
'factIds': ['F1'], 'claimType': 'inference'}]})
result = invoke(provider)
self.assertEqual(calls, ['native_synthesis', 'evidence_review'])
self.assertFalse(result['compositionRepairAttempted'])
self.assertNotIn('都已得到确认', result['answer'])
self.assertEqual(result['compositionDiagnostics']['rejectedBlockIndexes'], [0])
def test_chinese_kpi_quantities_remain_typed_even_when_structure_is_free(self):
for text in ('增加三个百分点{{F1}}', '达到百分之九十{{F1}}', '报废一百公斤{{F1}}', '投入增长两倍{{F1}}'):
with self.subTest(text=text), self.assertRaises(NativeCompositionError):
_validate_blocks({'blocks': [{'text': text, 'factIds': ['F1']}]},
[{'id': 'F1', 'text': '正式合格率95.00%。', 'evidenceRef': 'Q00000001'}], {})
def test_known_numeric_token_adds_its_verified_owner_but_review_still_rejects_false_claim(self):
facts = [{'id': 'F1', 'kind': 'overall', 'label': '产品甲', 'text': '产品甲合格率80.00%。', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'kind': 'overall', 'label': '产品乙', 'text': '产品乙合格率90.00%。', 'evidenceRef': 'Q00000002'}]
report = {'facts': facts, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [],
'missingEvidence': [], 'rejectedEvidenceRefs': []}
calls = []
def provider(request, timeout):
calls.append(request_phase(request))
if calls[-1] == 'evidence_review':
payload = fixture_payload(request)
self.assertEqual(payload['blocks'][0]['factIds'], ['F1', 'F2'])
return response({'reviews': [{'index': 0, 'supported': False}], 'missingGoalDescriptions': []})
payload = fixture_payload(request)
return response({'blocks': [{'text': '产品甲合格率是'+token(payload, '90.00%', 'F2'), 'factIds': ['F1'],
'evidenceRefs': ['Q00000001'], 'claimType': 'observation'}]})
with patch('nao_native_answer.build_native_report', return_value=report):
result = invoke(provider)
self.assertEqual(calls, ['native_synthesis', 'evidence_review'])
self.assertTrue(result['compositionDiagnostics']['bindingCompletions'])
self.assertNotIn('产品甲合格率是90.00%', result['answer'])
self.assertEqual(result['compositionDiagnostics']['rejectedBlockIndexes'], [0])
def test_business_numeric_goal_gaps_remain_concrete_and_unknown_refs_are_removed(self):
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'index': 0, 'supported': True}], 'missingGoalDescriptions': [
'尚未核对2026年9月前5个规格的0.62个百分点下降线索【6d4d4e5918】 QFFFFFFFF。']})
return response({'blocks': [{'text': '{{F1}}', 'factIds': ['F1']}]})
result = invoke(provider, context={'_agentTask': {'executionMode': 'native_agent'}})
self.assertTrue(result['compositionDiagnostics']['goalReviewCompleted'])
self.assertFalse(result['evidenceSufficient'])
gap = result['missingGoalDescriptions'][0]
self.assertIn('2026年9月前5个规格的0.62', gap)
self.assertNotIn('QFFFFFFFF', gap); self.assertNotIn('6d4d4e5918', gap)
def test_scalar_fact_slots_and_formal_composite_formula_do_not_create_false_scope_hypothesis(self):
docs = [{'evidenceRef': 'Q00000001', 'mode': 'query', 'result': {'kind': 'quality_metrics',
'period': {'year': 2026, 'month': 9}, 'filters': {}, 'summary': {
'compositeRate': .9506, 'black': {'rate': .97, 'qualifiedWeight': 97, 'totalWeight': 100, 'scrapWeight': 3},
'bright': {'rate': .98, 'qualifiedWeight': 98, 'totalWeight': 100, 'scrapWeight': 2}},
'coverage': {'sourceMonths': [{'year': 2026, 'month': 9, 'blackRows': 2, 'brightRows': 2}]}}}]
docs[0]['result']['calculationRules'] = {'supportsStageContribution': True, 'metricRules': {'compositeRate': {'formula': 'blackRate*brightRate'}}}
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
payload = fixture_payload(request)
self.assertIn('乘积', payload['metricSemantics']['compositeRate'])
self.assertNotIn('可能存在差异', payload['blocks'][1]['text'])
return response({'reviews': [{'index': 0, 'supported': True}, {'index': 1, 'supported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'text': '2026年9月综合合格率为{{F1}}。', 'factIds': ['F1']},
{'text': '黑片合格率为{{F2}},亮片合格率为{{F3}}。各阶段合格率均高于综合合格率,说明综合口径与分阶段口径在范围或归集方式上可能存在差异。',
'factIds': ['F2', 'F3'], 'claimType': 'inference'}]})
result = invoke(provider, docs=docs)
self.assertTrue(result['evidenceSufficient'], result['compositionDiagnostics'])
self.assertIn('综合合格率为95.06%', result['readingSummary']['lead'])
self.assertNotIn('为2026年', result['answer']); self.assertNotIn('。。', result['answer'])
self.assertIn('黑片合格率为97.00%', result['answer'])
self.assertIn('不能据此推断范围或归集方式存在差异', result['answer'])
self.assertTrue(result['compositionDiagnostics']['metricSemanticsCorrections'])
def test_complete_server_calendar_periods_bind_but_wrong_dates_and_kpis_do_not(self):
facts = [{'id': 'F1', 'kind': 'overall', 'label': '综合率', 'text': '2026年9月综合合格率95.00%。',
'evidenceRef': 'Q00000001', 'scope': {'period': {'year': 2026, 'month': 9}, 'comparisonPeriod': {'year': 2026, 'month': 8}}},
{'id': 'F2', 'kind': 'stage', 'label': '黑片', 'text': '2026年7–9月黑片合格率94.00%。',
'evidenceRef': 'Q00000002', 'scope': {'period': {'year': 2026, 'monthStart': 7, 'monthEnd': 9}}}]
tokens = _binding_inventory(facts)
for text, ids in (('2026年9月综合率按正式数据说明。', ['F1']),
('2026年7–9月的阶段结果已列出。', ['F2'])):
with self.subTest(text=text):
diagnostics = {}
parsed = _bind_exact_periods({'blocks': [{'text': text, 'factIds': ids}]}, tokens, diagnostics)
self.assertTrue(diagnostics['periodBindings'])
self.assertIn('2026年', _validate_blocks(parsed, facts, tokens)[0][0]['text'])
for text in ('2026年8月尚无核验', '2025年9月尚无核验', '2026年9月30日尚无核验',
'2026年9月合格率95.01%'):
with self.subTest(text=text), self.assertRaises(NativeCompositionError):
_validate_blocks(_bind_exact_periods({'blocks': [{'text': text, 'factIds': ['F1']}]}, tokens, {}), facts, tokens)
def test_literal_calendar_metric_answer_reaches_review_without_a_format_repair(self):
calls = []
def provider(request, timeout):
calls.append(request_phase(request))
if calls[-1] == 'evidence_review':
return response({'reviews': [{'index': 0, 'supported': True}], 'missingGoalDescriptions': []})
payload = fixture_payload(request)
current = next(fact for fact in payload['facts'] if fact['id'] == 'F5')
return response({'blocks': [{'text': '2026年9月综合合格率为'+token(payload, '95.00%', current['id'])+',可结合阶段数据继续核查。',
'factIds': [current['id']], 'claimType': 'observation'}]})
result = invoke(provider)
self.assertTrue(result['evidenceSufficient'], result.get('compositionDiagnostics'))
self.assertEqual(calls, ['native_synthesis', 'evidence_review'])
self.assertFalse(result['compositionRepairAttempted'])
self.assertIn('2026年9月', result['readingSummary']['lead'])
self.assertTrue(result['compositionDiagnostics']['periodBindings'])
def test_server_identifiers_bind_as_whole_strings_and_dimension_typos_stay_rejected(self):
a, b = '16.33X15.5X3.75+T', '17.85X16.9(4.225X4)X6.5+T'
facts = [{'id': 'F1', 'kind': 'product', 'label': a, 'text': a+';合格率95.00%,投入100.00 kg,报废5.00 kg。',
'evidenceRef': 'Q00000001', 'scope': {'dimension': 'product', 'filters': {}}},
{'id': 'F2', 'kind': 'supporting', 'label': b+'规模依据', 'text': b+';投入200.00 kg,报废10.00 kg。',
'evidenceRef': 'Q00000002', 'scope': {'dimension': 'product', 'filters': {}}}]
tokens = _binding_inventory(facts)
original = {'blocks': [{'text': '重点规格'+a+'与'+b+'是这两个规格,两项均应结合投入和报废核查。', 'factIds': ['F1', 'F2']}]}
diagnostics = {}
parsed = _bind_exact_identifiers(original, tokens, diagnostics)
blocks, _ = _validate_blocks(parsed, facts, tokens)
self.assertIn(a, blocks[0]['text']); self.assertIn(b, blocks[0]['text'])
self.assertEqual(len(diagnostics['identifierBindings']), 2)
for text, ids in (('重点规格'+a.replace('3.75', '3.76'), ['F1']),
('重点规格'+b, ['F1'])):
with self.subTest(text=text), self.assertRaises(NativeCompositionError):
_validate_blocks(_bind_exact_identifiers({'blocks': [{'text': text, 'factIds': ids}]}, tokens, {}), facts, tokens)
def test_numeric_customer_identifier_cannot_hide_an_unbound_kpi(self):
facts = [{'id': 'F1', 'kind': 'overall', 'label': '综合率', 'text': '客户800001,合格率95.00%。',
'evidenceRef': 'Q00000001', 'scope': {'filters': {'extraFilters': {'customerCode': ['800001']}}}}]
tokens = _binding_inventory(facts)
parsed = _bind_exact_identifiers({'blocks': [{'text': '客户800001应结合正式数据核查。', 'factIds': ['F1']}]}, tokens, {})
self.assertIn('客户800001', _validate_blocks(parsed, facts, tokens)[0][0]['text'])
with self.assertRaises(NativeCompositionError):
_validate_blocks(_bind_exact_identifiers({'blocks': [{'text': '合格率800001% {{F1}}', 'factIds': ['F1']}]}, tokens, {}), facts, tokens)
def test_confirmed_exclusion_identifier_binds_as_condition_and_not_measured_subject(self):
excluded = '18X15X4.1+Z'
facts = [{'id': 'F1', 'kind': 'overall', 'label': '总体', 'text': '排除'+excluded+';合格率95.00%。',
'evidenceRef': 'Q00000001', 'scope': {'filters': {'exclusions': {'product': [excluded]}}}},
{'id': 'F2', 'kind': 'overall', 'label': '另一范围', 'text': '合格率90.00%。',
'evidenceRef': 'Q00000002', 'scope': {'filters': {}}}]
tokens = _binding_inventory(facts)
self.assertTrue(any(item.get('role') == 'excluded_condition' and item['value'] == excluded for item in tokens.values()))
for text, ids in (('排除'+excluded, ['F1']),):
self.assertIn(excluded, _validate_blocks(_bind_exact_identifiers({'blocks': [{'text': text, 'factIds': ids}]}, tokens, {}), facts, tokens)[0][0]['text'])
for text, ids in (('排除'+excluded.replace('4.1', '4.2'), ['F1']), ('排除'+excluded, ['F2'])):
with self.subTest(text=text), self.assertRaises(NativeCompositionError):
_validate_blocks(_bind_exact_identifiers({'blocks': [{'text': text, 'factIds': ids}]}, tokens, {}), facts, tokens)
report = {'facts': facts, 'requiredFactIds': [], 'priorityFactIds': [], 'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
payload = fixture_payload(request)
fact = next(item for item in payload['facts'] if item['id'] == 'F1')
self.assertEqual(fact['identifierConditions'][0]['role'], 'excluded_condition')
return response({'reviews': [{'index': 0, 'supported': False}], 'missingGoalDescriptions': []})
return response({'blocks': [{'text': excluded+'的合格率95.00%。', 'factIds': ['F1']}]})
with patch('nao_native_answer.build_native_report', return_value=report):
result = invoke(provider)
self.assertNotIn(excluded+'的合格率95.00%', result['answer'])
self.assertEqual(result['compositionDiagnostics']['rejectedBlockIndexes'], [0])
def test_business_words_are_not_chinese_numbers_and_structural_counts_need_cited_objects(self):
facts = [{'id': 'F1', 'kind': 'category', 'label': '黑片·责废', 'text': '黑片责废增加。', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'kind': 'category', 'label': '黑片·料废', 'text': '黑片料废增加。', 'evidenceRef': 'Q00000001'},
{'id': 'F3', 'kind': 'stage', 'label': '黑片', 'text': '黑片合格率下降,投入增加,报废重量增加。', 'evidenceRef': 'Q00000001'}]
_validate_blocks({'blocks': [{'text': '关注重点规格和重点部门,两个主要上升类别均需核查。', 'factIds': ['F1', 'F2']}]}, facts, {})
_validate_blocks({'blocks': [{'text': '结合投入规模、报废重量和率三个维度。', 'factIds': ['F3']}]}, facts, {})
for text in ('三个类别值得核查', '两个主要方向值得核查'):
with self.subTest(text=text):
blocks, _ = _validate_blocks({'blocks': [{'text': text, 'factIds': ['F1', 'F2']}]}, facts, {})
self.assertFalse(blocks[0]['verbatim'])
def test_native_why_with_only_verbatim_baseline_still_gets_goal_review(self):
calls = []
def provider(request, timeout):
calls.append(request_phase(request))
if calls[-1] == 'evidence_review':
return response({'reviews': [{'index': 0, 'supported': True}],
'missingGoalDescriptions': ['尚未解释下降关系及重点对象的核查依据。']})
return response({'blocks': [{'text': '{{F1}}', 'factIds': ['F1']}]})
result = synthesize_native(base_url='https://offline.invalid/v1', api_key='fixture', model='fixture',
question='为什么合格率下降,请定位原因和重点规格?', evidence=evidence(),
context={'_agentTask': {'executionMode': 'native_agent'}}, cancel_event=threading.Event(), opener=provider)
self.assertEqual(calls, ['native_synthesis', 'evidence_review'])
self.assertFalse(result['evidenceSufficient'])
self.assertTrue(result['missingGoalDescriptions'])
def test_fact_pool_preserves_root_mechanism_scope_top_and_draft_focus_with_full_appendix(self):
facts = [{'id': 'F1', 'kind': 'overall', 'label': '综合合格率', 'text': '综合率96.00%→95.00%。',
'evidenceRef': 'Q00000001', 'scope': {'filters': {}, 'dimension': None}}]
facts.append({'id': 'F2', 'kind': 'stage', 'label': '黑片分子分母', 'text': '黑片合格重量90.00→160.00 kg,投入100.00→200.00 kg。',
'evidenceRef': 'Q00000001', 'scope': {'filters': {}, 'dimension': None}})
for scope in range(12):
ref = f'Q{scope+2:08X}'
for rank in range(20):
label = f'规格-{scope}-{rank}'
facts.append({'id': 'F' + str(len(facts)+1), 'kind': 'product', 'label': label,
'text': label + ';两期投入100.00→200.00 kg,报废10.00→30.00 kg;正式合格率90.00%→85.00%。' * 3,
'evidenceRef': ref, 'scope': {'filters': {'group': str(scope)}, 'dimension': 'product'},
'validity': {'verified': True}})
report = {'facts': [*facts, {**facts[0], 'id': 'F9999'}], 'requiredFactIds': [], 'priorityFactIds': [],
'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
pool, full, metadata = _select_fact_pool(report, '为什么下降', {'_agentTask': {'filters': {}}}, '重点规格为规格-11-19')
selected = {fact['id'] for fact in pool['facts']}
self.assertIn('F1', selected); self.assertIn('F2', selected)
self.assertTrue(any(fact['label'] == '规格-11-19' for fact in pool['facts']))
self.assertEqual(len({fact['evidenceRef'] for fact in pool['facts']}), 13)
self.assertLessEqual(metadata['modelEvidenceChars'], 54000)
self.assertEqual(len(full), len(facts))
self.assertGreater(len(full), len(pool['facts']))
def test_one_review_checks_missing_business_goals_without_extra_model_round(self):
calls = []
def provider(request, timeout):
calls.append(request_phase(request))
if calls[-1] == 'evidence_review':
payload = fixture_payload(request)
self.assertIn('question', payload)
return response({'reviews': [{'index': 0, 'supported': True}],
'missingGoalDescriptions': ['重点规格和具体缺陷尚未回答。']})
return response({'blocks': [{'text': '{{F1}}', 'factIds': ['F1']}]})
result = invoke(provider, context={'_agentTask': {'executionMode': 'native_agent', 'goal': '定位重点规格和具体缺陷'}})
self.assertEqual(calls, ['native_synthesis', 'evidence_review'])
self.assertFalse(result['evidenceSufficient'])
self.assertEqual(result['missingGoalDescriptions'], ['重点规格和具体缺陷尚未回答。'])
self.assertEqual(result['usedEvidenceRefs'], ['Q00000001'])
self.assertTrue(result['fullEvidenceFacts'])
def test_real_year_fragment_only_repairs_unbound_kpi_and_reviews_structure(self):
replay = json.loads((Path(__file__).parent / 'fixtures/native_year_literal_response.json').read_text())
docs = evidence()
docs[0]['result']['current']['period'] = {'year': 2026, 'monthStart': 9, 'monthEnd': 9}
docs[0]['result']['comparison']['period'] = {'year': 2026, 'monthStart': 8, 'monthEnd': 8}
replay['blocks'].extend([{'text': '两个主要方向值得继续核查{{F1}}', 'factIds': ['F1']},
{'text': '两条产线需要继续核查{{F1}}', 'factIds': ['F1']}])
replay['blocks'][0]['text'] += '另一个正式指标为99.99%。'
calls = []
def provider(request, timeout):
body = json.loads(request.data); calls.append(body)
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'index': index, 'supported': index == 0} for index in range(3)]})
if len(calls) == 1:
return response(replay)
repair = body['messages'][-1]['content']
self.assertIn('blocks[0].text', repair); self.assertIn('99.99%', repair)
self.assertNotIn('字面量"两个"', repair)
self.assertNotIn('字面量"两条"', repair)
fixed = copy.deepcopy(replay)
fixed['blocks'][0]['text'] = fixed['blocks'][0]['text'].replace('另一个正式指标为99.99%。', '').replace('{{F1}}显示,', '')
fixed['blocks'][1]['text'] = fixed['blocks'][1]['text'].replace('{{F1}}', '')
fixed['blocks'][2]['text'] = fixed['blocks'][2]['text'].replace('{{F1}}', '')
return response(fixed)
captured_report = build_native_report(docs)
# The captured model response predates singleton-range normalization;
# replay its original fact/token contract, not today's period renderer.
captured_report['facts'][0]['text'] = captured_report['facts'][0]['text'].replace('年8月', '年8–8月').replace('年9月', '年9–9月')
with patch('nao_native_answer.build_native_report', return_value=captured_report):
result = invoke(provider, docs=docs)
self.assertTrue(result['evidenceSufficient'], result.get('compositionDiagnostics'))
self.assertEqual(len(calls), 3)
self.assertTrue(result['compositionRepairAttempted'])
self.assertIn('需要进一步看黑片', result['answer'])
self.assertNotIn('两条产线', result['answer'])
self.assertTrue(result['compositionDiagnostics']['autoBindings'])
self.assertIn('2026年', result['rawModelResponses'][0])
def test_exact_year_binding_never_accepts_other_values_or_cross_fact_year(self):
facts = [{'id': 'F1', 'text': '2026年合格率95.00%。', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'text': '2025年9月合格率90.00%。', 'evidenceRef': 'Q00000002'}]
tokens = numeric_inventory(facts)
cases = [{'text': '2024年{{F1}}', 'factIds': ['F1']},
{'text': '95.00% {{F1}}', 'factIds': ['F1']},
{'text': '2026 {{F1}}', 'factIds': ['F1']},
{'text': '2026年{{N4}}月', 'factIds': ['F1', 'F2']}]
for block in cases:
with self.subTest(block=block):
diagnostics = {}
parsed = _bind_exact_year_literals({'blocks': [block]}, tokens, diagnostics)
with self.assertRaises(NativeCompositionError):
_validate_blocks(parsed, facts, tokens)
self.assertFalse(diagnostics.get('autoBindings'))
with self.assertRaisesRegex(NativeCompositionError, r'blocks\[0\].text.*95.00%.*\{\{N2\}\}'):
_validate_blocks({'blocks': [cases[1]]}, facts, tokens)
def test_only_proven_period_and_stage_connectors_are_allowed(self):
facts = [{'id': 'F1', 'text': '黑片与亮片按正式指标核查。', 'evidenceRef': 'Q00000001',
'scope': {'period': {'year': 2026, 'month': 9}, 'comparisonPeriod': {'year': 2026, 'month': 8}}}]
_validate_blocks({'blocks': [{'text': '两期对比应区分两阶段,两个阶段分别核查。', 'factIds': ['F1']}]}, facts, {})
for text in ('两个主要方向值得核查', '两期对比值得核查'):
blocks, _ = _validate_blocks({'blocks': [{'text': text, 'factIds': ['F1']}]},
[{**facts[0], 'scope': {}}], {})
self.assertFalse(blocks[0]['verbatim'])
def test_session_flag_routes_shared_synthesis_and_old_default_stays_legacy(self):
import nao_evidence_finalizer as finalizer
with patch('nao_evidence_finalizer.synthesize_native', return_value={'answer': 'native'}) as native:
result = finalizer.synthesize(base_url='fixture', api_key='fixture', model='fixture',
question='fixture', evidence=[], cancel_event=threading.Event(), context={'_autonomousAnswer': True})
self.assertEqual(result['answer'], 'native'); native.assert_called_once()
result = finalizer.synthesize(base_url='https://offline.invalid/v1', api_key='fixture', model='fixture', question='fixture',
evidence=evidence(), cancel_event=threading.Event(), opener=lambda *args, **kwargs: response(
{'answerBlocks': [{'text': '{{F1}}'}], 'evidenceSufficient': True, 'missingEvidence': []}))
self.assertNotIn('readingSummary', result)
def test_first_actual_binding_cannot_be_scale_signal_in_same_ranking_paragraph(self):
facts = [{'id': 'F1', 'text': '小样合格率0.00%。', 'label': '小样', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'text': '批量产品合格率上升至90.00%。', 'label': '批量产品', 'evidenceRef': 'Q00000001'}]
tokens = numeric_inventory(facts)
for text in ('{{F2}} {{F1}}', '小样与批量产品分别为{{N2}}、{{N1}}'):
with self.subTest(text=text), self.assertRaises(NativeCompositionError):
_validate_blocks({'blocks': [{'text': text, 'factIds': ['F2', 'F1']}]},
facts, tokens, ['F1'], ['F1'])
def test_review_downgrade_rechecks_priority_after_unordered_fact_ids(self):
facts = [{'id': 'F1', 'text': '小样合格率0.00%。', 'label': '小样', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'text': '批量产品合格率上升至90.00%。', 'label': '批量产品', 'evidenceRef': 'Q00000001'}]
report = {'facts': facts, 'requiredFactIds': ['F1'], 'priorityFactIds': ['F1'],
'scopes': [], 'missingEvidence': [], 'rejectedEvidenceRefs': []}
for claim in ('所有风险已排除。', '根因是设备磨损。'):
with self.subTest(claim=claim):
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'index': 0, 'supported': False}]})
return response({'blocks': [{'text': '小样合格率为{{N1}},批量产品也有规模记录。' + claim,
'factIds': ['F2', 'F1'], 'claimType': 'inference'}]})
with patch('nao_native_answer.build_native_report', return_value=copy.deepcopy(report)):
result = invoke(provider)
self.assertTrue(result['readingSummary']['lead'].startswith('小样合格率0.00%'))
self.assertNotIn('批量产品', result['readingSummary']['lead'])
self.assertTrue(result['compositionDiagnostics']['priorityLeadAdjusted'])
def test_accepts_reviewed_free_interpretation_and_expands_summary(self):
phases = []
def provider(request, timeout):
phases.append(request_phase(request))
if phases[-1] == 'evidence_review':
return response({'reviews': [{'index': 0, 'supported': True}]})
payload = fixture_payload(request)
return response({'blocks': [{'text': '本期综合合格率为' + token(payload, '95.00%')
+ ',低于基期的' + token(payload, '96.00%') + ',统计表现有所下降,需结合阶段指标核查。',
'factIds': ['F1'], 'claimType': 'inference', 'evidenceRefs': ['Q00000001']}]})
result = invoke(provider)
self.assertEqual(phases, ['native_synthesis', 'evidence_review'])
self.assertIn('统计表现有所下降', result['answer'])
self.assertIn('95.00%', result['readingSummary']['lead'])
self.assertNotIn('{{', result['answer'])
self.assertNotIn('99%', result['answer'])
self.assertEqual(result['readingSummary']['evidenceRefs'], ['Q00000001'])
self.assertTrue(result['evidenceSufficient'])
self.assertEqual(len(result['rawModelResponses']), 2)
def test_all_free_observations_are_reviewed_and_unsupported_observation_removed(self):
calls = []
def provider(request, timeout):
calls.append(request)
if len(calls) == 2:
return response({'reviews': [{'index': 0, 'supported': False}]})
return response({'blocks': [{'text': '所有产品质量都已完全恢复。', 'factIds': ['F1'], 'claimType': 'observation'}]})
result = invoke(provider)
self.assertEqual(len(calls), 2)
self.assertNotIn('都已完全恢复', result['answer'])
self.assertIn('下降1.00个百分点', result['answer'])
self.assertEqual(result['compositionDiagnostics']['rejectedBlockIndexes'], [0])
self.assertTrue(result['readingSummary']['notices'])
def test_unsupported_production_cause_becomes_explicit_hypothesis(self):
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'index': 0, 'supported': False}]})
return response({'blocks': [{'text': '根因是设备磨损导致合格率下降。', 'factIds': ['F1'], 'claimType': 'inference'}]})
result = invoke(provider)
self.assertIn('待验证假设', result['answer'])
self.assertIn('不能确认生产原因', result['answer'])
self.assertNotIn('根因是设备磨损', result['answer'])
self.assertIn('下降1.00个百分点', result['answer'])
def test_reviewed_observation_with_postposed_unverified_cause_is_preserved(self):
for boundary in ('现场根因尚未验证', '生产原因未确认', '具体现场原因待验证'):
calls = []
def provider(request, timeout):
calls.append(request_phase(request))
if calls[-1] == 'evidence_review':
return response({'reviews': [{'index': 0, 'supported': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'text': '本期综合合格率95.00%,上述变化的'+boundary+'。',
'factIds': ['F1'], 'claimType': 'hypothesis'}]})
with self.subTest(boundary=boundary):
result = invoke(provider)
self.assertEqual(calls, ['native_synthesis', 'evidence_review'])
self.assertIn('本期综合合格率95.00%,上述变化的'+boundary+'。', result['answer'])
self.assertNotIn('待验证假设', result['answer'])
self.assertEqual(result['compositionDiagnostics']['uncertaintyBoundaryBlockIndexes'], [0])
def test_postposed_uncertainty_does_not_hide_equipment_cause_or_false_relation(self):
for text in ('设备磨损导致本期综合合格率95.00%,现场根因尚未验证。',
'本期综合合格率95.00%高于基期96.00%,现场根因尚未验证。'):
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'index': 0, 'supported': False}], 'missingGoalDescriptions': []})
return response({'blocks': [{'text': text, 'factIds': ['F1'], 'claimType': 'hypothesis'}]})
with self.subTest(text=text):
result = invoke(provider)
self.assertNotIn(text, result['answer'])
if '设备' in text:
self.assertIn('待验证假设', result['answer'])
else:
self.assertNotIn('高于基期', result['answer'])
def test_unbound_number_gets_one_repair_and_failed_repair_is_explicit_gap(self):
calls = []
def provider(request, timeout):
calls.append(json.loads(request.data))
return response({'blocks': [{'text': '下降99%,归因于设备。', 'factIds': ['F1']}]})
result = invoke(provider)
self.assertEqual(len(calls), 2)
self.assertFalse(result['evidenceSufficient'])
self.assertEqual(result['finishReason'], 'native_synthesis_gap')
self.assertIn('格式未通过有限修复', result['answer'])
self.assertNotIn('99%', result['answer'])
self.assertEqual(len(result['rawModelResponses']), 2)
self.assertTrue(result['compositionDiagnostics']['formatRepairAttempted'])
self.assertFalse(result['compositionDiagnostics']['evidenceReviewAttempted'])
def test_valid_format_repair_uses_no_review_for_exact_facts(self):
calls = []
def provider(request, timeout):
calls.append(request)
return response('not json' if len(calls) == 1 else {'blocks': [{'text': '{{F1}}', 'factIds': ['F1']}]})
result = invoke(provider)
self.assertEqual(len(calls), 2)
self.assertTrue(result['evidenceSufficient'])
self.assertTrue(result['compositionRepairAttempted'])
self.assertFalse(result['compositionDiagnostics']['evidenceReviewAttempted'])
def test_unavailable_review_cannot_promote_free_observation(self):
calls = []
def provider(request, timeout):
calls.append(request)
return response({'blocks': [{'text': '所有产品都已恢复。', 'factIds': ['F1']}]}
if len(calls) == 1 else {'reviews': []})
result = invoke(provider)
self.assertEqual(len(calls), 2)
self.assertNotIn('都已恢复', result['answer'])
self.assertIn('部分解释未通过证据核验', result['answer'])
def test_unknown_reference_and_scope_mismatched_tokens_are_rejected(self):
facts = [{'id': 'F1', 'text': '产品甲合格率80%。', 'evidenceRef': 'Q00000001'},
{'id': 'F2', 'text': '产品乙合格率90%。', 'evidenceRef': 'Q00000002'}]
tokens = numeric_inventory(facts)
cases = [{'text': '{{N2}}', 'factIds': ['F1']},
{'text': '{{F1}}', 'factIds': ['F1'], 'evidenceRefs': ['Q99999999']},
{'text': '百分之九十', 'factIds': ['F1']},
{'text': '下降三个百分点', 'factIds': ['F1']},
{'text': '新值{{N999}}', 'factIds': ['F1']}]
for block in cases:
with self.subTest(block=block), self.assertRaises(NativeCompositionError):
_validate_blocks({'blocks': [block]}, facts, tokens)
def test_narrowed_evidence_is_available_but_broader_scope_is_rejected(self):
docs = evidence()
docs[0]['result']['filterBinding'] = {'group': '组甲', 'product': '产品甲'}
docs[0]['result']['releaseId'] = 'release'
broad = copy.deepcopy(docs[0]); broad['evidenceRef'] = 'Q00000002'
broad['result']['filterBinding'] = {}
context = {'releaseId': 'release', 'filters': {'group': '组甲'}, 'period': {'year': 2026, 'month': 9}}
report = build_native_report([*docs, broad], context)
self.assertTrue(report['facts'])
self.assertEqual({fact['evidenceRef'] for fact in report['facts']}, {'Q00000001'})
self.assertEqual(report['rejectedEvidenceRefs'], ['Q00000002'])
def test_real_tool_comparison_with_side_proofs_uses_root_without_static_scopes(self):
from nao_quality_tools import QualityTools
from test_nao_quality_tools import FakeApi, RELEASE
with tempfile.TemporaryDirectory() as folder:
tools = QualityTools(api_factory=lambda: FakeApi(), evidence_dir=folder)
result = tools.quality_data({'mode': 'compare', 'releaseId': RELEASE,
'period': {'year': 2026, 'month': 3}, 'comparisonPeriod': {'year': 2026, 'month': 1},
'filters': {'group': '汽车交通-国内', 'includeUnassigned': False}})
doc = json.loads(Path(result['evidencePath']).read_text())
for side in ('current', 'comparison'):
doc['result'][side]['protocolEvidence'] = {
'queryId': 'fixture-' + side, 'attemptId': 'fixture-attempt', 'sessionId': 'fixture-session',
'turnId': 'fixture-turn', 'revision': 1, 'executionId': 'fixture-execution',
'responseHash': 'a' * 64, 'nodeId': 'observed-' + side, 'scopeId': 'actual-frame'}
task = {'version': 1, 'kind': 'analysis', 'executionMode': 'native_agent',
'period': {'year': 2026, 'month': 3}, 'comparisonPeriod': {'year': 2026, 'month': 1},
'filters': {'group': '汽车交通-国内', 'includeUnassigned': False},
'dimensions': [], 'stages': [], 'fields': ['rate']}
context = {'releaseId': RELEASE, '_agentTask': task}
self.assertNotIn('queryPlan', task)
self.assertNotIn('_protocolScopes', context)
report = build_native_report([doc], context)
self.assertTrue(report['facts'])
self.assertEqual(report['rejectedEvidenceRefs'], [])
self.assertEqual(report['missingEvidence'], [])
self.assertTrue(any(fact['label'] == '阶段影响拆解' for fact in report['facts']))
outside = copy.deepcopy(doc)
outside['result']['filterBinding']['group'] = '汽车交通-海外'
self.assertFalse(build_native_report([outside], context)['facts'])
def test_extended_child_filters_retain_all_root_restrictions(self):
root = {'group': '组甲', 'extraFilters': {'customer': ['甲', '乙']},
'columnFilters': [{'field': '厚度', 'op': 'gte', 'value': 3}],
'exclusions': {'product': ['排除甲']}, 'includeUnassigned': False}
child = copy.deepcopy(root)
child['product'] = '产品甲'
child['extraFilters'] = {'customer': ['甲'], 'supplier': ['供方甲']}
child['columnFilters'].append({'field': '宽度', 'op': 'gte', 'value': 2})
child['exclusions']['product'].append('排除乙')
self.assertTrue(_within_filters(child, root))
child['includeUnassigned'] = True
self.assertFalse(_within_filters(child, root))
child['includeUnassigned'] = False
child['extraFilters']['customer'] = ['甲', '丙']
self.assertFalse(_within_filters(child, root))
def test_metric_numerator_denominator_values_are_available_without_static_dimensions(self):
docs = evidence()
for side in ('current', 'comparison'):
docs[0]['result'][side]['black'] = {'rate': .8, 'qualifiedWeight': 80, 'totalWeight': 100, 'scrapWeight': 20}
report = build_native_report(docs)
texts = '\n'.join(fact['text'] for fact in report['facts'])
self.assertIn('合格重量80.00 kg', texts)
self.assertIn('投入重量100.00 kg', texts)
self.assertIn('报废重量20.00 kg', texts)
self.assertNotIn('贡献20', texts)
def test_supported_numerator_denominator_explanation_is_not_reduced_to_fact_selection(self):
docs = evidence()
docs[0]['result']['comparison']['black'] = {'rate': .9, 'qualifiedWeight': 90,
'totalWeight': 100, 'scrapWeight': 10}
docs[0]['result']['current']['black'] = {'rate': .8, 'qualifiedWeight': 160,
'totalWeight': 200, 'scrapWeight': 40}
def provider(request, timeout):
if request_phase(request) == 'evidence_review':
return response({'reviews': [{'index': 0, 'supported': True}]})
payload = fixture_payload(request)
old = next(fact for fact in payload['facts'] if '2026年8月' in fact['text'] and '投入重量100.00' in fact['text'])
now = next(fact for fact in payload['facts'] if '2026年9月' in fact['text'] and '投入重量200.00' in fact['text'])
text = ('黑片投入由' + token(payload, '100.00', old['id']) + ' kg增至'
+ token(payload, '200.00', now['id']) + ' kg,合格重量由'
+ token(payload, '90.00', old['id']) + ' kg增至'
+ token(payload, '160.00', now['id'])
+ ' kg;投入分母扩张快于合格重量增长,与黑片合格率下降方向一致。这是指标关系解释,尚需现场核查。')
return response({'blocks': [{'text': text, 'factIds': [old['id'], now['id']], 'claimType': 'inference'}]})
result = invoke(provider, docs=docs)
self.assertIn('投入分母扩张快于合格重量增长', result['answer'])
self.assertIn('100.00 kg增至200.00 kg', result['answer'])
self.assertTrue(result['evidenceSufficient'])
self.assertEqual(result['compositionDiagnostics'].get('rejectedBlockIndexes', []), [])
def test_malformed_provider_response_is_retained_and_bounded(self):
calls = []
def provider(request, timeout):
calls.append(request)
return io.BytesIO(b'provider returned malformed envelope')
result = invoke(provider)
self.assertEqual(len(calls), 2)
self.assertEqual(result['rawModelResponses'], ['provider returned malformed envelope'] * 2)
self.assertFalse(result['evidenceSufficient'])
def test_native_ranking_lead_remains_lowest_rate_even_with_large_improving_signal(self):
side = lambda rate, scrap: {'rate': rate, 'scrapWeight': scrap,
'blackInputWeight': 100, 'brightInputWeight': 100}
doc = {'evidenceRef': 'Q00000001', 'mode': 'changes', 'result': {
'kind': 'quality_changes', 'releaseId': 'release', 'filters': {},
'currentPeriod': {'year': 2026, 'month': 9}, 'comparisonPeriod': {'year': 2026, 'month': 8},
'dimension': 'product', 'stage': None, 'sort': 'current_rate', 'completeRanking': True,
'totalRows': 2, 'rows': [{'rank': 1, 'key': '小样', 'label': '小样', 'rateDrop': 1,
'current': side(0, 1), 'comparison': side(1, 0)}],
'prioritySignals': {'currentScrap': [{'key': '批量产品', 'label': '批量产品', 'scrapDelta': 200,
'current': side(.9, 300), 'comparison': side(.8, 100)}]}}}
report = build_native_report([doc], question='哪些产品近期表现最差')
primary = report['priorityFactIds'][0]
signal = next(fact['id'] for fact in report['facts'] if '批量产品' in fact['text'])
cause_report = build_native_report([*evidence(), doc],
{'_agentTask': {'executionMode': 'native_agent', 'filters': {}}}, '为什么下降,请调查重点规格和具体缺陷')
self.assertEqual(cause_report['priorityFactIds'], [])
self.assertEqual(cause_report['requiredFactIds'], [])
cause_pool, _, _ = _select_fact_pool(cause_report, '为什么下降', {'_agentTask': {'filters': {}}})
self.assertEqual(cause_pool['facts'][0]['kind'], 'overall')
calls = []
def provider(request, timeout):
calls.append(request)
if len(calls) == 1:
return response({'blocks': [{'text': '{{' + signal + '}}', 'factIds': [signal]},
{'text': '{{' + primary + '}}', 'factIds': [primary]}]})
return response({'blocks': [{'text': '{{' + primary + '}}', 'factIds': [primary]}]})
result = synthesize_native(base_url='https://offline.invalid', api_key='fixture', model='fixture',
question='哪些产品近期表现最差', evidence=[doc], cancel_event=threading.Event(), opener=provider)
self.assertEqual(len(calls), 2)
self.assertIn('小样', result['readingSummary']['lead'])
self.assertIn('0.00%', result['readingSummary']['lead'])
self.assertNotIn('批量产品', result['readingSummary']['lead'])
self.assertTrue(result['evidenceSufficient'])
def test_monthly_trend_uses_row_and_parent_month_proof(self):
from test_agent_trend import doc
result = build_native_report([doc()])
text = '\n'.join(fact['text'] for fact in result['facts'])
self.assertIn('2026年1月', text)
self.assertIn('75.00%', text)
self.assertIn('2026年3月', text)
self.assertNotIn('2026年2月', text)
self.assertNotIn('2026年;', text)
damaged = doc(); damaged['result']['trend'][0]['coverage']['blackRows'] = 0
damaged['result']['trend'][0]['coverage']['brightRows'] = 0
damaged_text = '\n'.join(fact['text'] for fact in build_native_report([damaged])['facts'])
self.assertNotIn('75.00%', damaged_text)
def test_source_supplied_stage_influence_is_kept_distinct_from_rate_change(self):
docs = evidence()
for side in ('current', 'comparison'):
for stage in ('black', 'bright'):
docs[0]['result'][side][stage] = {'rate': .8, 'totalWeight': 100, 'qualifiedWeight': 80}
docs[0]['result']['stageDecomposition'] = {'status': 'available',
'blackContributionToCompositePP': -.7, 'brightContributionToCompositePP': -.3}
report = build_native_report(docs)
influence = next(fact for fact in report['facts'] if fact['label'] == '阶段影响拆解')
self.assertIn('-0.70个百分点', influence['text'])
self.assertIn('-0.30个百分点', influence['text'])
self.assertIn('不代表生产根因', influence['text'])
result = invoke(lambda *args, **kwargs: response({'blocks': [{
'text': '{{' + influence['id'] + '}}', 'factIds': [influence['id']]}]}), docs=docs)
self.assertNotIn('待验证假设', result['answer'])
self.assertFalse(result['compositionDiagnostics']['evidenceReviewAttempted'])
def test_format_repair_and_evidence_review_have_one_attempt_each(self):
phases = []
def provider(request, timeout):
phases.append(request_phase(request))
if len(phases) == 1:
return response('bad json')
if phases[-1] == 'evidence_review':
return response({'reviews': [{'index': 0, 'supported': True}]})
return response({'blocks': [{'text': '本期正式率低于基期,需继续核查阶段差异。',
'factIds': ['F1'], 'claimType': 'inference'}]})
result = invoke(provider)
self.assertEqual(phases, ['native_synthesis', 'native_synthesis', 'evidence_review'])
self.assertTrue(result['evidenceSufficient'])
self.assertTrue(result['compositionDiagnostics']['formatRepairAttempted'])
if __name__ == '__main__':
unittest.main()