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

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

716 lines
48 KiB
Python

"""Native tool loop boundaries, using actual tool adapters and captured-like APIs."""
import copy
import json
import sys
import tempfile
import unittest
import threading
from unittest.mock import patch
from pathlib import Path
sys.path[:0] = [str(Path(__file__).resolve().parents[1] / 'scripts'), str(Path(__file__).parent)]
from nao_native_exploration import prepare_step, NativeExplorationTools, _proofs, native_model_view
from nao_agent_task import normalize_task, plan_task
from nao_quality_tools import QualityToolError, tool_definition
from test_nao_protocol_execution import BoundApi, identity
from test_nao_quality_tools import RELEASE
from test_nao_agent_task import task_value
from test_nao_query_plan_raw import InvestigationApi
def root():
return {'version': 1, 'goal': '解释质量变化并根据结果调查', 'question': '9月份合格率下降原因',
'executionMode': 'native_agent', 'kind': 'analysis', 'period': {'year': 2026, 'month': 9},
'comparisonPeriod': {'year': 2026, 'month': 8}, 'filters': {}, 'fields': ['rate'],
'dimensions': [], 'stages': [], 'dimensionStages': {}, 'visualPreference': 'auto',
'verifySource': False, 'fullDetails': False, 'freshQuery': True,
'comparisonRequired': True, 'trendRequired': False}
class Delegated(BoundApi):
def __init__(self):
super().__init__()
self.steps, self.authorization_calls = [], 0
self.timeout, self.deadline_reserve_seconds = 30, None
def call(self, path, payload=None, method=None, *, node_id=None, scope_id=None):
if path == '/api/analysis/model-authorization':
self.authorization_calls += 1
return {}
if path == '/api/analysis/protocol/queries':
return {'scopeCoverage':[{'nodeId':step['node']['nodeId'], 'scopeId':'scope-'+step['node']['nodeId'],
'complete':True,'missingEvidence':[]} for step in self.steps], 'rootGoalGaps':[]}
if path == '/api/analysis/protocol/exploration/steps':
self.steps.append(copy.deepcopy(payload))
task = root()
task.update(comparisonRequired=payload['node']['comparisonRequired'])
task['filters'] = {}
node = payload['node']
selector = node['selector']
if selector['kind'] == 'catalog_members': task['filters'][selector['dimension']] = selector['names'][0]
return {'nodeId': node['nodeId'], 'scopeId': 'scope-' + node['nodeId'],
'task': task, 'scope': {'nodeId': node['nodeId'], 'task': task}}
response = super().call(path, payload=payload, method=method)
if path.startswith('/api/metrics'):
from urllib.parse import urlsplit, parse_qs
month = parse_qs(urlsplit(path).query).get('month', ['0'])[0]
response['protocolEvidence'] = {**identity(), 'nodeId': node_id, 'scopeId': scope_id,
'queryId': 'query-' + month, 'responseHash': 'a'*64}
return response
class NativeExplorationTests(unittest.TestCase):
def state(self): return {'releaseId': RELEASE, 'scopes': []}
def test_report_rank_is_available_without_a_preplanned_rank_dimension(self):
task=root();task.update(dimensions=['defect'],comparisonPeriod=None,
question='生成2026年1—10月总体质量分析报告,重点说明总体表现、月度变化和主要报废结构。')
schema=tool_definition(native_protocol=True,task=task,release_id=RELEASE)['inputSchema']
self.assertIn('rank',schema['properties']['mode']['enum'])
self.assertNotIn('investigate',schema['properties']['mode']['enum'])
def test_captured_rank_calls_explain_missing_metric_before_proposing_any_scope(self):
for dimension in ('department','product'):
with self.subTest(dimension=dimension), self.assertRaisesRegex(QualityToolError,'缺少metric'):
prepare_step({'mode':'rank','stage':'black','dimension':dimension,'limit':10},root(),self.state())
def test_public_rank_trace_retains_the_actual_metric_without_credentials(self):
from nao_quality_runtime import public_tool_call
call=public_tool_call('mcp_call',{'tool':'quality_data','arguments':{
'mode':'rank','metric':'totalWeight','stage':'black','dimension':'product','token':'private'}})
self.assertEqual(call['input']['metric'],'totalWeight')
self.assertEqual(call['label'],'查询重量排名')
self.assertNotIn('token',call['input'])
def test_batch_overview_uses_one_bound_scope_and_overall_alias_cannot_widen_parent(self):
task=root();task['filters']={'department':'A'}
args,proposal=prepare_step({'mode':'query','view':'group','filters':{'department':'__ALL__'}},task,self.state())
self.assertEqual(args['filters'],{'department':'A'})
self.assertEqual(args['period'],task['period'])
self.assertEqual(args['limit'],100)
self.assertEqual(proposal['node']['purpose'],'metrics')
self.assertEqual(proposal['node']['dimension'],'group')
self.assertEqual(proposal['node']['selector'],{'kind':'base'})
def test_inactive_rows_are_omitted_from_model_projection_without_recalculating_summary(self):
tools=NativeExplorationTools(delegated=None,evidence_dir=Path('/unused'),task=root(),identity={},
state_path=Path('/unused/state'),directory={'departments':['有效产业部']})
raw={'view':'department','summary':{'compositeRate':.95},'rows':[{'key':'有效产业部'},
{'key':'欧洲磁钢'},{'key':'美洲产业部'}],'rowCount':3,'rowsTruncated':False}
view=tools.active_result(raw)
self.assertEqual(view['rows'],[{'key':'有效产业部'}])
self.assertEqual(view['summary'],raw['summary'])
self.assertEqual(len(raw['rows']),3)
def test_no_published_rows_are_cached_as_no_data_with_original_proof(self):
from nao_quality_tools import SourceCoverageUnavailable
with tempfile.TemporaryDirectory() as folder:
p=Path(folder);api=Delegated()
tools=NativeExplorationTools(delegated=api,evidence_dir=p/'evidence',task=root(),
identity={**identity(),'releaseId':RELEASE},state_path=p/'state.json')
raw={'coverage':{'sourceMonths':[]},'protocolEvidence':{**identity(),'queryId':'empty-query'}}
with patch('nao_quality_tools.QualityTools.quality_data',side_effect=SourceCoverageUnavailable(root()['period'],raw)) as query:
first=tools.quality_data({'mode':'query'})
second=tools.quality_data({'mode':'query'})
self.assertEqual(query.call_count,1)
self.assertEqual(first['status'],'no_data')
self.assertIsNone(first['summary'])
self.assertEqual(first['protocolEvidence'],raw['protocolEvidence'])
self.assertTrue(second['reusedEvidence'])
@staticmethod
def source_task():
value = root()
value.update(kind='analysis', fields=[], dimensions=[], dimensionStages={}, stages=['black'],
period={'year': 2026, 'month': 9}, comparisonPeriod=None,
comparisonRequired=False, trendRequired=False, verifySource=True,
fullDetails=False)
return value
@staticmethod
def source_catalog():
return {'releaseId': RELEASE, 'sourceFields': [
{'key': 'src_responsibility_detail', 'label': '责废细项重量', 'stages': ['black'],
'columns': {'black': '原表责废细项重量', 'bright': None}},
{'key': 'src_black_total', 'label': '黑片报废合计', 'stages': ['black'],
'columns': {'black': '原表黑片报废合计', 'bright': None}},
]}
def test_model_view_removes_only_transport_and_identical_signal_duplicates(self):
row={'key':'甲','label':'甲','rank':1,'current':{'rate':0,'scrapWeight':None},
'comparison':{'rate':None,'scrapWeight':0}}
source={'kind':'quality_changes','rows':[row],'totalRows':9,'sort':'current_rate',
'prioritySignals':{'scale':[{'key':'甲','label':'甲','current':copy.deepcopy(row['current']),
'comparison':copy.deepcopy(row['comparison'])},{'key':'乙','current':{'rate':0.9}}]},
'evidenceRef':'Q12345678','protocolEvidence':{'queryId':'private'},'evidencePath':'private/path'}
original=copy.deepcopy(source);view=native_model_view(source)
def without_display(value):
if isinstance(value,list):return [without_display(item) for item in value]
if isinstance(value,dict):return {key:without_display(item) for key,item in value.items() if key!='formalDisplay'}
return value
self.assertEqual(without_display(view['rows']),[row]);self.assertEqual(view['totalRows'],9)
self.assertNotIn('formalDisplay',view['rows'][0]['current']) # Unknown is not zero.
self.assertEqual(view['rows'][0]['comparison']['formalDisplay']['scrapWeight'],
{'rawValue':'0','displayValue':'0.00','unit':'kg','metricRole':'scrapWeight','rawUnit':'kg','measureKind':'mass'})
self.assertEqual(source,original)
self.assertEqual(view['prioritySignals']['scale'][0]['sameAsReturnedRow'],'甲')
self.assertEqual(view['prioritySignals']['scale'][1],source['prioritySignals']['scale'][1])
self.assertNotIn('protocolEvidence',view);self.assertNotIn('evidencePath',view)
self.assertEqual(view['evidenceRef'],'Q12345678')
self.assertIn('protocolEvidence',source)
source['prioritySignals']['scale'][0]['rateDrop']=None
changed=native_model_view(source)
self.assertIn('rateDrop',changed['prioritySignals']['scale'][0])
self.assertIsNone(changed['prioritySignals']['scale'][0]['rateDrop'])
def test_official_category_share_names_its_actual_denominator_without_mutating_signed_values(self):
source={'kind':'quality_metrics','summary':{'bright':{'rate':.98,'scrapWeight':100,
'defects':[{'name':'加工废','weight':30,'rate':.6},{'name':'镀废','weight':20,'rate':.4}]}}}
original=copy.deepcopy(source);view=native_model_view(source)['summary']['bright']
self.assertEqual(view['rate'],.98)
self.assertEqual(view['defects'][0]['shareOfListedCategoryWeight'],.6)
self.assertNotIn('rate',view['defects'][0])
self.assertEqual(view['categoryShareBasis'],'sum_of_listed_category_weights')
self.assertEqual(source,original)
def test_explicit_cause_cannot_silently_become_a_static_recipe(self):
from nao_native_planning import explicit_investigation
self.assertTrue(explicit_investigation('为什么下降?请自行深挖'))
self.assertFalse(explicit_investigation('取消原因分析,只查合格率'))
native = task_value(executionMode='native_agent', dimensions=[], stages=[], fields=['rate'],
period={'year':2026,'month':9}, comparisonPeriod={'year':2026,'month':8},
comparisonRequired=True, trendRequired=False)
wrong = copy.deepcopy(native); wrong.pop('executionMode')
with patch('nao_agent_task._model_json', side_effect=[wrong,native]) as model:
task = plan_task(base_url='http://offline.invalid', api_key='fixture', model='fixture',
question='为什么下降?请深入分析', context={'_protocol':{'version':1,'autonomousEnabled':True}},
cancel_event=threading.Event())
self.assertEqual(task['executionMode'],'native_agent')
self.assertNotIn('queryPlan', task)
self.assertEqual(model.call_count,2)
self.assertNotIn('黑片投入重量排名后查看前两项',model.call_args.kwargs['system'])
def test_clear_ranking_rejects_native_route_before_confirmation(self):
from nao_native_planning import explicit_batch_ranking
question='2026年9月汽车交通哪些规格合格率最低?按本期综合合格率从低到高列出前5个,同时说明投入和报废规模。'
self.assertTrue(explicit_batch_ranking(question))
self.assertFalse(explicit_batch_ranking('产品合格率最低的原因是什么?请深入调查'))
self.assertFalse(explicit_batch_ranking('不要排名,只查产品合格率'))
native=task_value(kind='analysis',executionMode='native_agent',dimensions=[],dimensionStages={},
fields=['rate'],stages=[],period={'year':2026,'month':9},comparisonRequired=True,
comparisonPeriod={'year':2026,'month':8})
batch={**native,'dimensions':['product'],'fields':['rate','totalWeight','scrapWeight'],
'dimensionStages':{'product':[]},'queryPlan':[{
'nodeId':'rank','purpose':'changes','dimension':'product','sort':'current_rate','limit':5,
'fields':['rate','totalWeight','scrapWeight'],'stages':[],
'selector':{'kind':'base'},'comparisonRequired':True,'visualType':'bar','visualPreference':'auto'}]}
batch.pop('executionMode')
with patch('nao_agent_task._model_json',side_effect=[native,batch]):
task=plan_task(base_url='https://offline.invalid/v1',api_key='fixture',model='fixture',
question=question,context={'_protocol':{'version':1,'autonomousEnabled':True}},
cancel_event=threading.Event())
self.assertNotEqual(task.get('executionMode'),'native_agent')
self.assertEqual(task['queryPlan'][0]['sort'],'current_rate')
self.assertEqual(task['queryPlan'][0]['limit'],5)
self.assertEqual(task['planRecovery']['modelAttempts'],2)
def test_root_cannot_invent_domestic_and_explicit_group_gets_its_catalog_parent(self):
from nao_native_planning import validate_root_scope
catalog = {'groups':['汽车交通-国内','汽车交通-海外'],
'groupDepartments':{'汽车交通-国内':'新能源汽车磁钢','汽车交通-海外':'新能源汽车磁钢'}}
narrowed = root(); narrowed['filters'] = {'group':'汽车交通-国内'}
with self.assertRaisesRegex(ValueError,'擅自缩'):
validate_root_scope(copy.deepcopy(narrowed), '汽车交通为什么下降', catalog)
valid = validate_root_scope(copy.deepcopy(narrowed), '那国内为什么下降', catalog)
self.assertEqual(valid['filters'],{'group':'汽车交通-国内','department':'新能源汽车磁钢'})
inherited = validate_root_scope(copy.deepcopy(narrowed),'继续分析原因',catalog,{'group':'汽车交通-国内'})
self.assertEqual(inherited['filters']['group'],'汽车交通-国内')
new = copy.deepcopy(narrowed); new['newTask'] = True
with self.assertRaises(ValueError):
validate_root_scope(new,'换个问题,公司为什么下降',catalog,{'group':'汽车交通-国内'})
from dbgpt_trial_data import encoded_selection
multi = root(); multi['filters'] = {'group':encoded_selection(['汽车交通-国内','汽车交通-海外'])}
with self.assertRaises(ValueError):
validate_root_scope(copy.deepcopy(multi),'公司为什么下降',catalog)
explicit = validate_root_scope(copy.deepcopy(multi),'汽车交通国内和海外为什么下降',catalog)
self.assertEqual(explicit['filters']['department'],'新能源汽车磁钢')
self.assertEqual(validate_root_scope(copy.deepcopy(multi),'继续分析',catalog,multi['filters'])['filters'],explicit['filters'])
def test_explicit_authorized_department_cannot_disappear_from_root(self):
from nao_native_planning import validate_root_scope
catalog={'departments':['新能源汽车磁钢','消费电子-3C'],'groups':[], 'groupDepartments':{}}
for question in ('汽车交通为什么下降','分析汽车交通不良原因','新能源汽车磁钢9月合格率', '3C合格率',
'只分析汽车交通,不需要全公司数据','仅看汽车交通,不和消费电子比较',
'汽车交通为什么下降,不看其他部门'):
with self.subTest(question=question), self.assertRaises(ValueError):
validate_root_scope({'filters':{}},question,catalog)
for question in ('汽车交通不要了,改看公司整体','排除汽车交通看其余',
'汽车交通和消费电子比较','比较3C与汽车交通',
'汽车交通相比其他产业部如何','不含汽车交通,查全公司'):
with self.subTest(question=question):
self.assertEqual(validate_root_scope({'filters':{}},question,catalog)['filters'],{})
self.assertEqual(validate_root_scope({'filters':{'department':'新能源汽车磁钢'}},
'分析汽车交通不良原因',catalog)['filters']['department'],'新能源汽车磁钢')
with self.assertRaises(ValueError):
validate_root_scope({'filters':{'department':'消费电子-3C'}},'汽车交通为什么下降',catalog)
# A product containing the suffix is not a department mention.
self.assertEqual(validate_root_scope({'filters':{}},'查规格A3CX1',catalog)['filters'],{})
def test_department_omission_is_repaired_before_confirmation(self):
value=task_value(kind='analysis',executionMode='native_agent',dimensions=[],dimensionStages={},
fields=['rate'],stages=[],period={'year':2026,'month':9})
context={'_protocol':{'version':1,'autonomousEnabled':True},
'_businessCatalog':{'departments':['新能源汽车磁钢','消费电子-3C']}}
with patch('nao_agent_task._model_json',side_effect=[value,{**value,'filterUpdates':{'department':'新能源汽车磁钢'}}]):
task=plan_task(base_url='https://offline.invalid/v1',api_key='fixture',model='fixture',
question='请深挖汽车交通为什么下降',context=context,cancel_event=threading.Event())
self.assertEqual(task['filters']['department'],'新能源汽车磁钢')
self.assertEqual(task['planRecovery']['modelAttempts'],2)
self.assertEqual(task['planRecovery']['errors'][0]['field'],'filterUpdates.department')
def test_negated_broad_scope_cannot_bypass_department_repair(self):
value=task_value(kind='analysis',executionMode='native_agent',dimensions=[],dimensionStages={},
fields=['rate'],stages=[],period={'year':2026,'month':9})
context={'_protocol':{'version':1,'autonomousEnabled':True},
'_businessCatalog':{'departments':['新能源汽车磁钢','消费电子-3C']}}
for question in ('只分析汽车交通,不需要全公司数据','仅看汽车交通,不和消费电子比较',
'汽车交通为什么下降,不看其他部门'):
with self.subTest(question=question), patch('nao_agent_task._model_json',side_effect=[
value,{**value,'filterUpdates':{'department':'新能源汽车磁钢'}}]):
task=plan_task(base_url='https://offline.invalid/v1',api_key='fixture',model='fixture',
question=question,context=context,cancel_event=threading.Event())
self.assertEqual(task['filters']['department'],'新能源汽车磁钢')
self.assertEqual(task['planRecovery']['modelAttempts'],2)
def test_business_cross_check_is_not_raw_file_permission(self):
value = task_value(kind='analysis', dimensions=[], stages=[], fields=['rate'],
period={'year': 2026, 'month': 9}, executionMode='native_agent',
verifySource=True, fullDetails=True, trendRequired=False)
context = {'_protocol': {'version': 1, 'autonomousEnabled': True}}
task = normalize_task(value, '核对主要线索,先给分析', context)
self.assertFalse(task['verifySource']); self.assertFalse(task['fullDetails'])
explicit = normalize_task(value, '请核验原表,并逐行读取全部原始记录', context)
self.assertTrue(explicit['verifySource']); self.assertTrue(explicit['fullDetails'])
context['_protocol']['previousTask'] = explicit
cancelled = normalize_task(value, '取消原表核验,不要逐行读取', context)
self.assertFalse(cancelled['verifySource']); self.assertFalse(cancelled['fullDetails'])
for wording in ('后面不要原始数据,只分析统计', '别再看原始行了', '原始数据不用了'):
cancelled = normalize_task(value, wording, context)
self.assertFalse(cancelled['verifySource'], wording)
self.assertFalse(cancelled['fullDetails'], wording)
for wording in ('不需要每行数据', '不要数据行', '不要每一条记录'):
cancelled = normalize_task(value, wording, context)
self.assertFalse(cancelled['fullDetails'], wording)
retained = normalize_task(value, '不要逐行记录,继续核验原表', context)
self.assertFalse(retained['fullDetails'])
self.assertTrue(retained['verifySource'])
for wording, flag in (('请核验不良品原表', 'verifySource'),
('分析不合格原因并核验原表', 'verifySource'),
('请逐行读取不合格的原始记录', 'fullDetails')):
allowed = normalize_task(value, wording, {'_protocol': {'version': 1, 'autonomousEnabled': True}})
self.assertTrue(allowed[flag], wording)
def test_disabled_comparison_does_not_revive_an_optional_old_comparison_date(self):
value = task_value(kind='metrics', dimensions=[], fields=['rate'], stages=[],
period={'year':2026,'month':10}, comparisonRequired=False,
comparisonPeriod={'year':2026,'month':9})
task = normalize_task(value,'只查十月,不比较',{'_protocol':{'version':1,'autonomousEnabled':True}})
self.assertIsNone(task['comparisonPeriod'])
def test_new_confirmed_goal_does_not_restore_cancelled_historical_ranking(self):
value=task_value(goal='只查询综合合格率,取消排名及规模要求',kind='metrics',
dimensions=[],stages=[],fields=['rate'],period={'year':2026,'month':9},comparisonRequired=False)
previous={'goal':'按低率排名五个规格及规模','rootGoal':'按低率排名五个规格及规模','kind':'analysis','filters':{}}
with patch('nao_agent_task._model_json',return_value=value):
planned=plan_task(base_url='http://offline.invalid',api_key='fixture',model='fixture',
question='取消排名和规模,只查合格率',previous=previous,
context={'_protocol':{'version':1,'autonomousEnabled':True},
'_serverHistory':[{'question':previous['goal'],'evidenceRefs':['Q12345678']}]},
cancel_event=threading.Event())
self.assertEqual(planned['rootGoal'],value['goal'])
def test_planner_failure_clarification_keeps_explicit_negative_product_constraint(self):
context={'year':2026,'month':9,'_protocol':{'version':1,'autonomousEnabled':True},
'_businessCatalog':{'explicitlyExcludedProducts':['18X15X4.1+Z'],
'productMatch':{'status':'ambiguous','query':'18x15x4','truncated':False,
'candidates':[{'name':'18X15X4+T'},{'name':'18X15X4+Z'}]}}}
with patch('nao_agent_task._model_json',return_value=None):
task=plan_task(base_url='http://offline.invalid',api_key='fixture',model='fixture',
question='18X15X4合格率,不要包含18X15X4.1',context=context,cancel_event=threading.Event())
self.assertTrue(task['needsClarification'])
self.assertEqual(task['period'],{'year':2026,'month':9})
self.assertEqual(task['filters']['exclusions']['product'],['18X15X4.1+Z'])
def test_root_scope_cannot_be_changed_or_old_filter_reintroduced(self):
task = root(); task['filters'] = {'department': '甲'}
with self.assertRaises(QualityToolError):
prepare_step({'mode': 'compare', 'filters': {'department': '乙'}}, task, self.state())
args, _ = prepare_step({'mode': 'compare'}, task, self.state())
self.assertEqual(args['filters'], {'department': '甲'})
self.assertEqual(task['filters'], {'department': '甲'})
def test_next_observation_can_choose_a_new_dimension_and_another_stage(self):
task = root()
first, a = prepare_step({'mode': 'changes', 'dimension': 'group', 'sort': 'rate_drop'}, task, self.state())
second, b = prepare_step({'mode': 'defects', 'stage': 'bright', 'filters': {'product': '规格乙'}}, task, self.state())
self.assertEqual(a['node']['dimension'], 'group')
self.assertEqual(b['node']['selector']['names'], ['规格乙'])
self.assertEqual(b['node']['stages'], ['bright'])
self.assertNotEqual(a['node']['nodeId'], b['node']['nodeId'])
self.assertEqual(task, root())
def test_raw_reads_require_explicit_root_request(self):
with self.assertRaises(QualityToolError):
prepare_step({'mode': 'investigate', 'stage': 'black', 'action': 'rows'}, root(), self.state())
def test_exact_product_without_defect_uses_its_defect_breakdown_not_product_ranking(self):
args, step=prepare_step({'mode':'defects','stage':'black','dimension':'product',
'filters':{'product':'规格甲'}},root(),self.state())
self.assertEqual(args['dimension'],'defect')
self.assertEqual(step['node']['dimension'],'defect')
self.assertEqual(args['filters'],{'product':'规格甲'})
unchanged, _=prepare_step({'mode':'defects','stage':'black','dimension':'product','defect':'责废缺角重量'},root(),self.state())
self.assertEqual(unchanged['dimension'],'product')
def test_raw_tools_use_protocol_endpoints_and_exact_defaults(self):
task = root(); task['fullDetails'] = True; task['verifySource'] = True
args, proposal = prepare_step({'mode': 'details', 'stage': 'black'}, task, self.state())
self.assertEqual(args['mode'], 'investigate')
self.assertEqual((args['action'], args['offset'], args['recordFilter']), ('rows', 0, 'all'))
self.assertEqual(proposal['node']['purpose'], 'records')
args, proposal = prepare_step({'mode': 'investigate', 'stage': 'bright', 'action': 'source'}, task, self.state())
self.assertEqual(args['limit'], 100)
self.assertEqual(proposal['node']['purpose'], 'source')
def test_native_source_fields_map_only_exact_current_catalog_keys_labels_and_columns(self):
task = self.source_task()
catalog = self.source_catalog()
args, proposal = prepare_step({'mode': 'investigate', 'action': 'source', 'stage': 'black',
'fields': ['责废细项重量']}, task, self.state(), source_catalog=catalog)
self.assertEqual(args['fields'], ['src_responsibility_detail'])
self.assertEqual(proposal['node']['sourceFields'], {'black': ['src_responsibility_detail']})
args, proposal = prepare_step({'mode': 'investigate', 'action': 'source', 'stage': 'black',
'fields': ['src_black_total']}, task, self.state(), source_catalog=catalog)
self.assertEqual(args['fields'], ['src_black_total'])
self.assertEqual(proposal['node']['sourceFields'], {'black': ['src_black_total']})
duplicate_label = copy.deepcopy(catalog)
duplicate_label['sourceFields'].append({'key': 'src_duplicate', 'label': '责废细项重量',
'stages': ['black'], 'columns': {'black': '另一原列', 'bright': None}})
for bad_catalog, bad_field, error in (
(catalog, '未经授权的字段', '不在当前releaseId'),
(catalog, 'src_bright_only', '不在当前releaseId'),
(duplicate_label, '责废细项重量', '不唯一')):
with self.subTest(field=bad_field), self.assertRaisesRegex(QualityToolError, error):
prepare_step({'mode': 'investigate', 'action': 'source', 'stage': 'black',
'fields': [bad_field]}, task, self.state(), source_catalog=bad_catalog)
stale = {**catalog, 'releaseId': 'older-release'}
with self.assertRaisesRegex(QualityToolError, '不属于当前releaseId'):
prepare_step({'mode': 'investigate', 'action': 'source', 'stage': 'black',
'fields': ['src_responsibility_detail']}, task, self.state(), source_catalog=stale)
def test_source_scope_rejects_unsupported_actions_and_category_without_registering(self):
task = self.source_task()
for action in ('overview', 'fields'):
with self.subTest(action=action), self.assertRaisesRegex(QualityToolError, '只支持action=source或rows'):
prepare_step({'mode': 'investigate', 'action': action, 'stage': 'black'}, task, self.state())
with self.assertRaisesRegex(QualityToolError, '不支持category筛选.*categoryReconciliation'):
prepare_step({'mode': 'investigate', 'action': 'source', 'stage': 'black',
'category': '责废'}, task, self.state())
with self.assertRaisesRegex(QualityToolError, '未签发sourceRevisionId'):
prepare_step({'mode': 'investigate', 'action': 'source', 'stage': 'black',
'sourceRevisionId': 'revision-2'}, task, self.state())
# Category filtering remains a supported defect query and is not silently stripped.
_, proposal = prepare_step({'mode': 'defects', 'stage': 'black', 'dimension': 'defect',
'category': '责废'}, root(), self.state())
self.assertEqual(proposal['node']['category'], '责废')
with tempfile.TemporaryDirectory() as folder:
path = Path(folder); api = Delegated()
tools = NativeExplorationTools(delegated=api, evidence_dir=path/'evidence', task=task,
identity={**identity(), 'releaseId': RELEASE}, state_path=path/'state.json')
with self.assertRaisesRegex(QualityToolError, 'categoryReconciliation'):
tools.quality_data({'mode': 'investigate', 'action': 'source', 'stage': 'black',
'category': '责废'})
self.assertEqual(api.steps, [])
def test_dsh_schema_uses_only_protocol_supported_investigation_actions(self):
legacy = tool_definition()
native = tool_definition(native_protocol=True)
self.assertEqual(legacy['inputSchema']['properties']['action']['enum'],
['overview', 'rows', 'fields', 'source'])
self.assertEqual(native['inputSchema']['properties']['action']['enum'], ['source', 'rows'])
self.assertIn('正式指标概览使用query', native['description'])
self.assertIn('字段目录使用catalog', native['description'])
self.assertIn('categoryReconciliation', native['inputSchema']['properties']['category']['description'])
query_rule = next(rule for rule in native['inputSchema']['allOf']
if rule.get('if', {}).get('properties', {}).get('mode', {}).get('const') == 'query')
query_forbidden = [clause['not']['required'][0] for clause in query_rule['then']['allOf']]
self.assertIn('stage', query_forbidden)
self.assertIn('category', query_forbidden)
native_raw_scope = next(rule for rule in native['inputSchema']['allOf']
if rule.get('if', {}).get('properties', {}).get('action', {}).get('enum') == ['source', 'rows'])
self.assertEqual([clause['not']['required'][0] for clause in native_raw_scope['then']['allOf']],
['category', 'sourceRevisionId'])
def test_source_query_signs_catalog_stable_key_and_sends_only_signed_original_column(self):
source_fields = [{'key': 'src_responsibility_detail', 'label': '责废细项重量',
'stages': ['black'], 'columns': {'black': '原表责废细项重量', 'bright': None}}]
fine_catalog = {'kind': 'fine_defect_catalog', 'grain': 'source_defect_weight',
'fields': [{'stage': 'black', 'key': '责废尺寸重量', 'label': '责废尺寸重量',
'category': '责废', 'available': True}],
'categories': [{'stage': 'black', 'category': '责废', 'available': True}]}
class SourceApi(Delegated):
def __init__(self):
super().__init__()
self.registered = []
self.source_payloads = []
self.registered_scopes = []
def call(self, path, payload=None, method=None, *, node_id=None, scope_id=None):
if path == '/api/analysis/model-authorization':
self.authorization_calls += 1
return {}
if path == '/api/releases':
return {'currentReleaseId': RELEASE, 'items': [
{'id': RELEASE, 'name': 'Published', 'status': 'PUBLISHED'}]}
if path == '/api/options':
return {'years': [2026], 'departments': [], 'groups': [], 'products': [], 'projectGroups': []}
if path == '/api/analysis/tools/catalog':
return {'kind': 'analysis_catalog', 'visibleWeightFields': [], 'visibleDefectFields': [],
'dimensions': [], 'topDimensions': [], 'grouping': ['overall'], 'limitations': [],
'sourceFields': copy.deepcopy(source_fields), 'fineDefects': copy.deepcopy(fine_catalog)}
if path == '/api/analysis/protocol/exploration/steps':
self.registered.append(copy.deepcopy(payload))
node = payload['node']
task = NativeExplorationTests.source_task()
task['stages'] = copy.deepcopy(node['stages'])
selected = node.get('sourceFields')
if selected:
task['sourceFields'] = copy.deepcopy(selected)
task['investigationFields'] = {
stage: [next(row['columns'][stage] for row in source_fields if row['key'] == key)
for key in keys]
for stage, keys in selected.items()}
registered = {'nodeId': node['nodeId'], 'scopeId': 'scope-' + node['nodeId'],
'task': task, 'scope': {'nodeId': node['nodeId'], 'scopeId': 'scope-' + node['nodeId'],
'purpose': node['purpose'], 'task': task}}
self.registered_scopes.append(registered)
return copy.deepcopy(registered)
if path == '/api/analysis/investigate':
self.source_payloads.append(copy.deepcopy(payload))
period = {key: payload[key] for key in ('year', 'month', 'quarter', 'monthStart', 'monthEnd')
if key in payload}
context = {**period, 'releaseId': RELEASE, 'stage': payload['stage'], 'category': None,
'sourceRevisionId': None, 'includeUnassigned': False,
'department': payload.get('department'), 'group': payload.get('group'),
'product': payload.get('product'), 'projectGroup': payload.get('projectGroup'),
'extraFilters': payload.get('extraFilters', {}), 'columnFilters': payload.get('columnFilters', []),
'exclusions': payload.get('exclusions', {})}
proof = {**identity(), 'queryId': 'source-query', 'responseHash': 'a' * 64,
'nodeId': node_id, 'scopeId': scope_id}
return {'kind': 'quality_investigation', 'action': 'source', 'releaseId': RELEASE,
'context': context, 'totalRows': 1, 'aggregateComplete': True,
'categories': [{'category': '责废', 'categoryReconciliation': {'status': 'INCOMPLETE'}}],
'lineage': [], 'selectedFields': copy.deepcopy(payload.get('fields')),
'sourceVerification': {'expectedRows': 1, 'complete': True, 'verifiedRows': 1,
'missingRows': 0, 'mismatchRows': 0, 'allSelectedValuesMatchStored': True},
'protocolEvidence': proof}
if path == '/api/analysis/protocol/queries':
return {'scopeCoverage': [{'nodeId': row['nodeId'], 'scopeId': row['scopeId'],
'complete': True, 'missingEvidence': []} for row in self.registered_scopes], 'rootGoalGaps': []}
return super().call(path, payload, method, node_id=node_id, scope_id=scope_id)
with tempfile.TemporaryDirectory() as folder:
path = Path(folder)
api = SourceApi()
tools = NativeExplorationTools(delegated=api, evidence_dir=path/'evidence', task=self.source_task(),
identity={**identity(), 'releaseId': RELEASE}, state_path=path/'state.json')
tools.quality_data({'mode': 'catalog'})
result = tools.quality_data({'mode': 'investigate', 'action': 'source', 'stage': 'black',
'fields': ['责废细项重量']})
self.assertEqual(len(api.registered), 1)
signed_node = api.registered[0]['node']
self.assertEqual(signed_node['sourceFields'], {'black': ['src_responsibility_detail']})
self.assertEqual(api.source_payloads[0]['fields'], ['原表责废细项重量'])
self.assertNotIn('category', api.source_payloads[0])
self.assertEqual(result['action'], 'source')
self.assertEqual(result['category'], None)
self.assertEqual(_proofs(result)[0]['nodeId'], signed_node['nodeId'])
def test_native_source_fields_require_catalog_from_current_release_before_registration(self):
class NoCatalog(Delegated):
pass
with tempfile.TemporaryDirectory() as folder:
path = Path(folder); api = NoCatalog()
tools = NativeExplorationTools(delegated=api, evidence_dir=path/'evidence', task=self.source_task(),
identity={**identity(), 'releaseId': RELEASE}, state_path=path/'state.json')
with self.assertRaisesRegex(QualityToolError, '当前releaseId的catalog'):
tools.quality_data({'mode': 'investigate', 'action': 'source', 'stage': 'black',
'fields': ['src_responsibility_detail']})
self.assertEqual(api.steps, [])
def test_multilevel_child_inherits_verified_parent(self):
state = self.state(); state['scopes'] = [{'filters': {'group': '甲'}, 'queryIds': ['q1']}]
_, step = prepare_step({'mode': 'defects', 'stage': 'black', 'filters': {'group': '甲', 'product': '乙'}}, root(), state)
self.assertEqual(step['parentQueryIds'], ['q1'])
self.assertEqual(step['node']['selector'], {'kind': 'catalog_members', 'dimension': 'product', 'names': ['乙']})
def test_equivalent_single_month_syntax_keeps_the_confirmed_frame(self):
task = root()
args, _ = prepare_step({'mode': 'compare', 'period': {'year': 2026, 'monthStart': 9, 'monthEnd': 9}}, task, self.state())
self.assertEqual(args['period'], {'year': 2026, 'month': 9})
with self.assertRaises(QualityToolError):
prepare_step({'mode': 'compare', 'period': {'year': 2026, 'monthStart': 8, 'monthEnd': 9}}, task, self.state())
def test_invalid_shape_is_rejected_before_registering_a_scope(self):
with tempfile.TemporaryDirectory() as folder:
api = Delegated(); p = Path(folder)
tools = NativeExplorationTools(delegated=api, evidence_dir=p/'evidence', task=root(),
identity={**identity(), 'releaseId': RELEASE}, state_path=p/'state.json')
with self.assertRaises(QualityToolError):
tools.quality_data({'mode': 'compare', 'dimension': 'product'})
self.assertEqual(api.steps, [])
def test_catalog_reads_only_authorized_metadata_and_formal_reads_still_register_scope(self):
fine_catalog = {
'kind': 'fine_defect_catalog', 'grain': 'source_defect_weight',
'fields': [
{'stage': 'black', 'key': '责废尺寸重量', 'label': '责废尺寸重量',
'category': '责废', 'sourceField': '责废尺寸重量',
'grain': 'source_defect_weight', 'available': True},
{'stage': 'bright', 'key': '镀废重量', 'label': '镀废重量',
'category': '镀废', 'sourceField': '镀废重量',
'grain': 'source_defect_weight', 'available': True},
],
'categories': [
{'stage': 'black', 'category': '责废', 'available': True},
{'stage': 'bright', 'category': '镀废', 'available': True},
],
}
class CatalogApi(Delegated):
def call(self, path, payload=None, method=None, *, node_id=None, scope_id=None):
result = super().call(path, payload, method, node_id=node_id, scope_id=scope_id)
if path == '/api/analysis/tools/catalog':
result = {**result, 'fineDefects': copy.deepcopy(fine_catalog)}
return result
with tempfile.TemporaryDirectory() as folder:
p = Path(folder); api = CatalogApi()
tools = NativeExplorationTools(delegated=api, evidence_dir=p/'evidence', task=root(),
identity={**identity(), 'releaseId': RELEASE}, state_path=p/'state.json')
catalog = tools.quality_data({'mode': 'catalog'})
self.assertEqual(catalog['periods']['availableMonthsInLatestYear'], [])
self.assertEqual(catalog['periods']['availableMonthsSource'], 'not_scanned')
self.assertEqual(catalog['fineDefects']['endpoint'], 'GET /api/analysis/tools/catalog')
self.assertEqual(catalog['fineDefects']['fields'], fine_catalog['fields'])
self.assertEqual(catalog['fineDefects']['scanned'], False)
self.assertNotIn('protocolEvidence', catalog)
narrowed = tools.quality_data({'mode': 'catalog', 'stage': 'black'})
self.assertEqual([item['stage'] for item in narrowed['fineDefects']['fields']], ['black'])
self.assertEqual([item['stage'] for item in narrowed['fineDefects']['categories']], ['black'])
paths = [call[1].split('?', 1)[0] for call in api.calls]
self.assertTrue(set(paths) <= {
'/api/analysis/model-authorization', '/api/releases', '/api/analysis/tools/catalog', '/api/options'
}, paths)
self.assertNotIn('/api/metrics', paths)
self.assertNotIn('/api/analysis/defects', paths)
for _ in range(3):
duplicate = tools.quality_data({'mode': 'catalog'})
self.assertTrue(duplicate['reusedEvidence'])
state = json.loads((p/'state.json').read_text())
self.assertEqual(state['noProgress'], 0)
self.assertFalse(state.get('halted', False))
tools.quality_data({'mode': 'compare'})
self.assertEqual(len(api.steps), 1)
self.assertTrue(any(path.startswith('/api/metrics') for _, path, _ in api.calls))
def test_actual_compare_preserves_both_proofs_and_duplicate_reads_are_cached(self):
with tempfile.TemporaryDirectory() as folder:
api = Delegated(); p = Path(folder)
tools = NativeExplorationTools(delegated=api, evidence_dir=p/'evidence', task=root(),
identity={**identity(), 'releaseId': RELEASE}, state_path=p/'state.json')
result = tools.quality_data({'mode': 'compare'})
self.assertEqual(len(_proofs(result)), 2)
self.assertEqual(len(api.steps), 1)
for _ in range(3):
duplicate = tools.quality_data({'mode': 'compare'})
self.assertTrue(duplicate['reusedEvidence'])
self.assertEqual(duplicate['evidenceRef'], result['evidenceRef'])
self.assertEqual(len(api.steps), 1)
self.assertEqual(api.authorization_calls, 4)
with self.assertRaisesRegex(QualityToolError, '停止'):
tools.quality_data({'mode': 'compare'})
def test_new_execution_never_reuses_previous_attempt_cache(self):
with tempfile.TemporaryDirectory() as folder:
api = Delegated(); p = Path(folder)
kwargs = dict(delegated=api, evidence_dir=p/'evidence', task=root(), state_path=p/'state.json')
a = NativeExplorationTools(**kwargs, identity={**identity(), 'releaseId': RELEASE})
first = a.quality_data({'mode': 'compare'})
b = NativeExplorationTools(**kwargs, identity={**identity(), 'executionId': 'new-execution', 'releaseId': RELEASE})
second = b.quality_data({'mode': 'compare'})
self.assertNotEqual(first['evidenceRef'], second['evidenceRef'])
self.assertEqual(len(api.steps), 2)
def test_explicit_refresh_reexecutes_but_identical_responses_are_still_no_progress(self):
with tempfile.TemporaryDirectory() as folder:
p=Path(folder); api=Delegated()
tools=NativeExplorationTools(delegated=api,evidence_dir=p/'evidence',task=root(),
identity={**identity(),'releaseId':RELEASE},state_path=p/'state.json')
first=tools.quality_data({'mode':'compare'})
for _ in range(3):
checked=tools.quality_data({'mode':'compare','freshQuery':True})
self.assertNotEqual(first['evidenceRef'],checked['evidenceRef'])
self.assertIn('没有新增',checked['investigationNotice'])
self.assertEqual(len(api.steps),1)
with self.assertRaisesRegex(QualityToolError,'停止'):
tools.quality_data({'mode':'compare','freshQuery':True})
def test_record_pages_share_authorized_scope_but_have_distinct_cached_evidence(self):
task = root()
task.update(fullDetails=True, comparisonRequired=False, comparisonPeriod=None, stages=['black'])
class Pages(InvestigationApi):
timeout = 30
deadline_reserve_seconds = None
def __init__(self):
super().__init__(total_rows=165)
self.registered = []
def call(self, path, payload=None, method=None, **kwargs):
if path == '/api/analysis/model-authorization': return {}
if path == '/api/analysis/protocol/queries':
return {'scopeCoverage':[{'nodeId':s['nodeId'],'scopeId':s['scopeId'],
'complete':self.query_count >= 2,'missingEvidence':[] if self.query_count >= 2 else ['后续页尚未读取']}
for s in self.scopes], 'rootGoalGaps':[]}
if path == '/api/analysis/protocol/exploration/steps':
self.registered.append(copy.deepcopy(payload))
self.add_scope(payload['node']['nodeId'], 'record-scope', 'records', task)
scope = self.scopes[-1]
return {'nodeId':scope['nodeId'], 'scopeId':scope['scopeId'], 'task':scope['task'], 'scope':scope}
return super().call(path, payload, method, **kwargs)
with tempfile.TemporaryDirectory() as folder:
p = Path(folder); api = Pages()
tools = NativeExplorationTools(delegated=api, evidence_dir=p/'evidence', task=task,
identity={**identity(), 'releaseId':RELEASE}, state_path=p/'state.json')
first = tools.quality_data({'mode':'details', 'stage':'black', 'offset':0})
last = tools.quality_data({'mode':'details', 'stage':'black', 'offset':100})
self.assertEqual(len(api.registered), 1)
self.assertFalse(first['investigationCoverage']['scopeComplete'])
self.assertTrue(last['investigationCoverage']['scopeComplete'])
reads = [c for c in api.calls if c['path'] == '/api/analysis/investigate']
self.assertEqual([c['payload']['offset'] for c in reads], [0,100])
self.assertEqual(len({(c['nodeId'],c['scopeId']) for c in reads}), 1)
self.assertNotEqual(first['evidenceRef'], last['evidenceRef'])
cached = tools.quality_data({'mode':'details', 'stage':'black', 'offset':100})
self.assertTrue(cached['reusedEvidence'])
self.assertEqual(api.query_count, 2)
tools.quality_data({'mode':'details', 'stage':'black', 'offset':100, 'freshQuery':True})
self.assertEqual(api.query_count, 3)
self.assertEqual(len(api.registered), 1)
if __name__ == '__main__': unittest.main()