Files
quality_analyse/verification/test_source_verification_repair.py
T
lyun ff7bbe3e46 同步:完善质量分析工作台并更新主题字体
同步已上线的智能分析执行协议、来源查询、图表报告与管理页面配套代码。
补齐数据库迁移、运行依赖和回归测试,保留原有指标计算口径。
统一午夜黑及其他主题的控件细节、中文和业务数字字体。
排除本地业务数据、凭据、预览、发布与验收产物。

验证:生产构建通过,320项前端回归通过;本轮仅同步Git,不再次部署。
2026-10-30 22:40:58 +08:00

186 lines
12 KiB
Python

import copy
import io
import sys
import threading
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
sys.path[:0]=[str(Path(__file__).resolve().parents[1]/'scripts'),str(Path(__file__).resolve().parent)]
import nao_quality_runtime as runtime
from nao_agent_task import TaskUnavailable, bind_task, normalize_task, plan_task
from nao_analysis_coverage import plan_missing_source_queries
from nao_evidence_finalizer import coverage_sufficient
from test_nao_agent_task import task_value
from test_agent_runtime import comparison
RELEASE='33333333-3333-4333-8333-333333333333'
CURRENT={'year':2026,'month':9}
PREVIOUS={'year':2026,'month':8}
FILTERS={'department':'新能源汽车磁钢'}
def review(source, rows, *, evidence=True):
return {'aligned':True,'requiresEvidence':evidence,
'requiresSourceVerification':source,'requiresAllSourceRows':rows,'requiresTrend':False}
def plan_value(**kwargs):
return task_value(dimensions=['group'],dimensionStages={'group':[]},stages=['black','bright'],
fields=['rate','scrapWeight'],period=CURRENT,comparisonPeriod=PREVIOUS,
comparisonRequired=True,filterUpdates=FILTERS,**kwargs)
def source_document(stage, group=None, *, bad=False, missing_comparison=False):
filters={**FILTERS,**({'group':group} if group else {})}
def side(period):
context={'releaseId':RELEASE,**period,**filters,'stage':stage,'includeUnassigned':False}
return {'context':context,'aggregateComplete':True,'totalRows':2,
'sourceVerification':{'complete':not bad,'allSelectedValuesMatchStored':not bad,
'expectedRows':2,'verifiedRows':1 if bad else 2,'missingRows':1 if bad else 0,'mismatchRows':0}}
result={'kind':'quality_investigation','action':'source','releaseId':RELEASE,'stage':stage,
'period':CURRENT,'comparisonPeriod':PREVIOUS,'filters':filters,'current':side(CURRENT)}
if not missing_comparison:result['comparison']=side(PREVIOUS)
return {'mode':'investigate','evidenceRef':'QAAAAAAAA','result':result}
class SemanticSourceIntentTests(unittest.TestCase):
def run_plan(self, question, replies, context=None):
with patch('nao_agent_task._model_json',side_effect=replies) as call:
result=plan_task(base_url='https://offline.invalid/v1',api_key='synthetic',model='m',
question=question,context=context or {},cancel_event=threading.Event())
return result,call
def test_unrequested_source_read_is_replanned_not_silently_rewritten(self):
initial=plan_value(verifySource=True)
corrected=plan_value()
context={'_serverQuestions':[{'question':'9月合格率为什么下降?','status':'PARTIAL'}]}
result,call=self.run_plan('主要是国内还是国外的',[initial,review(False,False),corrected,review(False,False)],context)
self.assertFalse(result['verifySource']);self.assertEqual(call.call_count,4)
self.assertTrue(call.call_args_list[2].kwargs['payload']['invalidPlan']['verifySource'])
self.assertIn('verifySource',call.call_args_list[2].kwargs['payload']['validationFeedback'])
def test_explicit_source_and_all_rows_requirements_are_preserved(self):
source=plan_value(verifySource=True)
result,_=self.run_plan('请对照原表核验两期数据',[source,review(True,False)])
self.assertTrue(result['verifySource'])
rows=plan_value(fullDetails=True)
result,_=self.run_plan('请逐条列出所有原始记录',[rows,review(False,True)])
self.assertTrue(result['fullDetails'])
def test_explicit_source_omitted_by_planner_is_replanned(self):
first=plan_value()
corrected=plan_value(verifySource=True)
result,call=self.run_plan('请对照原表核验',[first,review(True,False),corrected,review(True,False)])
self.assertTrue(result['verifySource']);self.assertEqual(call.call_count,4)
def test_missing_read_requirement_fields_and_explicit_conflict_fail_closed(self):
source=plan_value(verifySource=True)
with self.assertRaises(TaskUnavailable):
self.run_plan('请核验原表',[source,{'aligned':True,'requiresEvidence':True}]*3)
with self.assertRaises(TaskUnavailable):
self.run_plan('请核验原表',[plan_value(),review(False,False)]*3)
def test_explicit_cancellation_does_not_force_source_read(self):
result,_=self.run_plan('不要核对原表,只看合格率',[plan_value(),review(False,False)])
self.assertFalse(result['verifySource'])
def test_natural_first_turn_source_request_is_reviewed_even_when_plan_omits_flag(self):
question='把上传的xlsx中的数值与系统记录逐项比对'
result,call=self.run_plan(question,[plan_value(),review(True,False),plan_value(verifySource=True),review(True,False)])
self.assertTrue(result['verifySource']);self.assertEqual(call.call_count,4)
def test_previous_source_requirement_and_explicit_cancellation_follow_current_intent(self):
history={'_serverQuestions':[{'question':'把上传的xlsx中的数值与系统记录逐项比对','status':'PARTIAL'}]}
result,_=self.run_plan('按刚才要求继续,主要看国内还是海外',
[plan_value(),review(True,False),plan_value(verifySource=True),review(True,False)],history)
self.assertTrue(result['verifySource'])
result,_=self.run_plan('不用核对原表,只看合格率',[plan_value(),review(False,False)],history)
self.assertFalse(result['verifySource'])
def test_positive_optional_review_fields_are_never_ignored(self):
history={'_serverQuestions':[{'question':'将上传的资料和系统的值一项项核一遍','status':'PARTIAL'}]}
first=plan_value();corrected=plan_value(verifySource=True)
with patch('nao_agent_task._model_json',side_effect=[first,review(True,False),corrected,review(True,False)]) as call:
result=plan_task(base_url='https://offline.invalid/v1',api_key='synthetic',model='m',
question='按刚才要求继续,主要看国内还是海外',context=history,cancel_event=threading.Event(),semantic_review=True)
self.assertTrue(result['verifySource']);self.assertEqual(call.call_count,4)
class ParentSourceRepairTests(unittest.TestCase):
def setUp(self):
self.task=normalize_task(task_value(kind='source_verify',dimensions=[],dimensionStages={},
stages=['black','bright'],fields=[],period=CURRENT,comparisonPeriod=PREVIOUS,
comparisonRequired=True,filterUpdates=FILTERS),'对照原表核验')
self.context=bind_task({'releaseId':RELEASE},self.task)
root=comparison();root['releaseId']=RELEASE;root['filterBinding']=FILTERS
self.root={'mode':'compare','evidenceRef':'QBBBBBBBB','result':root}
self.children=[source_document('black','汽车交通-国内'),source_document('bright','汽车交通-国内'),
source_document('black','汽车交通-海外')]
def test_child_scopes_do_not_satisfy_parent_and_two_stages_cover_both_periods(self):
docs=[self.root,*self.children]
queries=plan_missing_source_queries(docs,self.task,self.context)
self.assertEqual([q['stage'] for q in queries],['black','bright'])
for q in queries:
self.assertEqual(q['filters'],FILTERS);self.assertEqual(q['period'],CURRENT)
self.assertEqual(q['comparisonPeriod'],PREVIOUS);self.assertEqual(q['action'],'source')
self.assertFalse(coverage_sufficient(docs,self.task['question'],self.context))
docs.append(source_document('black'));self.assertFalse(coverage_sufficient(docs,self.task['question'],self.context))
self.assertEqual([q['stage'] for q in plan_missing_source_queries(docs,self.task,self.context)],['bright'])
docs.append(source_document('bright'));self.assertTrue(coverage_sufficient(docs,self.task['question'],self.context))
def test_missing_period_or_mismatched_source_stays_incomplete(self):
for doc in (source_document('bright',bad=True),source_document('bright',missing_comparison=True)):
docs=[self.root,source_document('black'),doc]
self.assertFalse(coverage_sufficient(docs,self.task['question'],self.context))
self.assertEqual([q['stage'] for q in plan_missing_source_queries(docs,self.task,self.context)],['bright'])
def test_runtime_executes_both_parent_reads_with_longer_bounded_timeout(self):
docs=[self.root,*self.children];seen=[];api=MagicMock()
def read(arguments):
seen.append((copy.deepcopy(arguments),api.timeout));result=source_document(arguments['stage']);docs.append(result);return result
request={'actorId':'actor','sessionId':'session','releaseId':RELEASE,'question':self.task['question'],'context':self.context}
with patch('nao_evidence_finalizer.evidence_summaries',side_effect=lambda *a,**k:list(docs)),\
patch('nao_agent_task.plan_repairs',side_effect=AssertionError('parent checks should be planned directly')),\
patch.object(runtime,'DelegatedApi') as delegated,patch('nao_quality_tools.QualityTools') as tools,\
patch('nao_runtime_policy.remaining_seconds',return_value=1000):
delegated.return_value.login.return_value=api;tools.return_value.quality_data.side_effect=read
runtime.complete_agent_evidence(task=self.task,request=request,actor_root=Path('/offline/session'),
access_path=Path('/offline/access'),manager=MagicMock(),connection=('offline','synthetic','m'),
accumulator=runtime.ConversationAccumulator(),prior_files=frozenset(),cancel_event=threading.Event(),emit=lambda e:None)
self.assertEqual([q['stage'] for q,_ in seen],['black','bright'])
self.assertTrue(all(q['filters']==FILTERS and timeout==180 for q,timeout in seen))
self.assertTrue(coverage_sufficient(docs,self.task['question'],self.context))
def test_finalization_time_reserve_prevents_false_completion(self):
docs=[self.root,*self.children];seen=[];api=MagicMock()
request={'actorId':'actor','sessionId':'session','releaseId':RELEASE,'question':self.task['question'],'context':self.context}
with patch('nao_evidence_finalizer.evidence_summaries',side_effect=lambda *a,**k:list(docs)),\
patch.object(runtime,'DelegatedApi') as delegated,patch('nao_quality_tools.QualityTools') as tools,\
patch('nao_runtime_policy.remaining_seconds',return_value=90):
delegated.return_value.login.return_value=api;tools.return_value.quality_data.side_effect=lambda args:seen.append(args)
runtime.complete_agent_evidence(task=self.task,request=request,actor_root=Path('/offline/session'),
access_path=Path('/offline/access'),manager=MagicMock(),connection=('offline','synthetic','m'),
accumulator=runtime.ConversationAccumulator(),prior_files=frozenset(),cancel_event=threading.Event(),emit=lambda e:None)
self.assertEqual(seen,[]);self.assertFalse(coverage_sufficient(docs,self.task['question'],self.context))
def test_each_http_request_recalculates_source_budget(self):
timeouts=[]
class Opener:
def open(self, request, timeout):
timeouts.append(timeout);return io.BytesIO(b'{}')
api=runtime.DelegatedApi('/offline/access',opener=Opener(),timeout=180)
api.deadline_reserve_seconds=90
access=SimpleNamespace(release_id=RELEASE,token='synthetic',query_url='http://127.0.0.1/offline')
with patch.object(runtime,'load_access_file',return_value=access),\
patch('nao_runtime_policy.remaining_seconds',side_effect=[250,100,90]):
api.call('/api/options?releaseId='+RELEASE,method='GET')
api.call('/api/options?releaseId='+RELEASE,method='GET')
with self.assertRaises(runtime.RuntimeErrorSafe):api.call('/api/options?releaseId='+RELEASE,method='GET')
self.assertEqual(timeouts,[160,10])
if __name__=='__main__':unittest.main()