统一:DSH 智能分析、Top100 与图表静默恢复

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

验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。
仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
This commit is contained in:
2026-11-12 11:57:41 +08:00
parent cc8832c8e5
commit f1a2fbd494
166 changed files with 26281 additions and 2248 deletions
+554 -1
View File
@@ -2,6 +2,7 @@
import copy
import io
import json
import re
import sys
import tempfile
import threading
@@ -12,7 +13,7 @@ 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
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
@@ -65,6 +66,514 @@ def token(payload, value, fact_id='F1'):
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': [],
@@ -87,6 +596,50 @@ class NativeAnswerTests(unittest.TestCase):
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'}