统一: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
@@ -0,0 +1,195 @@
import copy
import json
import sys
import threading
import unittest
from pathlib import Path
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[1]
sys.path[:0] = [str(ROOT/'scripts'), str(Path(__file__).parent)]
from dsh_row_selection import selection_hint, select_rows, ranking_chart, validate_selection
from dsh_model_input import canonical_proposal, planning_input, compact_tool_result
from nao_agent_task import normalize_task, plan_task
from nao_native_exploration import prepare_step
from nao_quality_tools import QualityToolError, QualityTools, tool_definition
from test_nao_agent_task import task_value
from test_nao_quality_tools import RELEASE
from test_native_exploration import root
QUESTION = '帮我筛选,10月份合格率最低TOP20产品。且重量大于1000KG,并生成报告'
def selection(limit=20, stage='any'):
value = selection_hint(QUESTION)
value['limit'] = limit
value['weightFilter']['stage'] = stage
return value
def population():
rows = [{'key': f'P{i:03d}', 'label': f'产品{i:03d}', 'compositeRate': .5 + i/1000,
'black': {'rate': .99-i/10000, 'totalWeight': 1001 if i % 2 else 10},
'bright': {'rate': .98, 'totalWeight': 1001 if not i % 2 else 10}} for i in range(150)]
rows.insert(0, {'key': 'LOW_WEIGHT', 'compositeRate': .01, 'black': {'totalWeight': 1000}, 'bright': {'totalWeight': 1000}})
return {'kind': 'quality_metrics', 'releaseId': RELEASE, 'view': 'product',
'period': {'year': 2026, 'month': 10}, 'filters': {},
'rows': rows, 'rowCount': len(rows), 'rowsReturned': len(rows), 'rowsTruncated': False}
class AggregateSelectionTests(unittest.TestCase):
def test_captured_failure_is_bound_as_aggregate_condition_not_source_filter(self):
captured = task_value(period={'year':2026,'month':10}, fields=['rate'], stages=['black'], dimensions=['product'], filterUpdates={'extraFilters':[{'key':'totalWeight','operator':'gt','value':1000}]})
captured.update(needsClarification=True, clarification='请确认黑片、亮片还是任一阶段投入重量?')
before = copy.deepcopy(captured)
value = canonical_proposal(captured, question=QUESTION)
task = normalize_task(value, QUESTION, {'_businessCatalog': {'years': [2026]}})
self.assertTrue(task['needsClarification'])
self.assertEqual(task['aggregateSelection']['weightFilter']['value'], 1000)
self.assertNotIn('extraFilters', task['filters'])
self.assertEqual(captured, before)
def test_clarification_can_resume_same_month_top_and_report_request(self):
previous = root()
previous.update(needsClarification=True, aggregateSelection=selection(stage=None), period={'year': 2026, 'month': 10})
context = {'_agentTask': previous}
for answer, expected in [('黑片or亮片', 'any'), ('我的选择:任一阶段重量大于1000KG', 'any'), ('两阶段均满足', 'all'), ('黑片和亮片都要大于1000', 'all'), ('亮片', 'bright'), ('黑片', 'black')]:
fixed = canonical_proposal(task_value(), question=answer, context=context)
self.assertEqual(fixed['aggregateSelection']['weightFilter']['stage'], expected)
self.assertEqual(fixed['aggregateSelection']['requestQuestion'], QUESTION)
self.assertEqual(fixed['period'], {'year': 2026, 'month': 10})
self.assertFalse(fixed['needsClarification'])
hint = planning_input({'question': QUESTION})['aggregateSelectionHint']
self.assertIsNone(hint['weightFilter']['stage'])
def test_weight_only_model_alias_uses_existing_rank_contract_without_losing_conditions(self):
question='2026年10月,按亮片投入重量由高到低列出产品Top100,不生成报告。'
value=task_value(fields=['totalWeight'],stages=['bright'],dimensions=['product'],
aggregateSelection={'dimension':'product','metric':'totalWeight','rateStage':'bright','direction':'desc','limit':100,'requestQuestion':question})
fixed=canonical_proposal(value,question=question)
self.assertNotIn('aggregateSelection',fixed)
self.assertEqual(fixed['fields'],['totalWeight'])
self.assertEqual(fixed['stages'],['bright'])
predicate=copy.deepcopy(value);predicate['aggregateSelection']['weightFilter']={'metric':'totalWeight','operator':'gt','value':1000,'stage':'bright'}
self.assertIn('aggregateSelection',canonical_proposal(predicate,question=question))
def test_filter_precedes_sort_and_top_and_does_not_recalculate_composite(self):
source = population(); before = copy.deepcopy(source)
selected = select_rows(source, selection())
self.assertEqual(selected['eligibleRows'], 150)
self.assertEqual([row['key'] for row in selected['rows']], [f'P{i:03d}' for i in range(20)])
self.assertEqual(selected['rows'][0]['selectedRate'], .5)
self.assertEqual(source, before)
all_stages = select_rows(source, selection(stage='all'))
self.assertEqual(all_stages['returnedRows'], 0)
def test_top100_and_chart_panels_keep_every_rank(self):
source = population(); task = {'aggregateSelection': selection(100)}
view = compact_tool_result(source, task=task)
self.assertEqual(len(view['rows']), 100)
self.assertEqual(view['rowCount'], 151)
self.assertEqual(view['selectionDisplay']['eligibleRows'], 150)
charts = ranking_chart({'result': source, 'evidenceRef': 'Q12345678'}, task, 'test-turn')
self.assertEqual(len(charts), 4)
self.assertEqual(sum(len(chart['categories']) for chart in charts), 100)
self.assertTrue(all(chart['series'][0]['key'] == chart['metric'] for chart in charts))
self.assertEqual(charts[-1]['categories'][-1]['label'], '100. 产品099')
def test_missing_invalid_rows_and_ties_do_not_become_zero_or_fake_rates(self):
source = population()
source['rows'][1]['compositeRate'] = None
source['rows'][2]['compositeRate'] = float('nan')
source['rows'][3]['compositeRate'] = source['rows'][4]['compositeRate']
selected = select_rows(source, selection())
self.assertEqual(selected['missingRateRows'], 2)
self.assertEqual([row['key'] for row in selected['rows'][:2]], ['P002', 'P003'])
source['rowsTruncated'] = True
with self.assertRaises(ValueError): select_rows(source, selection())
def test_limit_bounds_and_batch_scope_keep_root_filters(self):
for count in (0, 101):
with self.assertRaises(ValueError): validate_selection(selection(count))
task = root(); task.update(aggregateSelection=selection(100), comparisonPeriod=None)
args, proposal = prepare_step({'mode': 'query', 'view': 'product'}, task, {'releaseId': RELEASE, 'scopes': []})
self.assertEqual(proposal['node']['dimension'], 'product')
self.assertEqual(args['limit'], 500)
self.assertEqual(args['period'], task['period'])
schema = tool_definition(native_protocol=True, task=task, release_id=RELEASE)['inputSchema']
self.assertIn('product', schema['properties']['view']['enum'])
task.update(stages=['black'], fields=['totalWeight'], dimensions=['product'])
args, proposal = prepare_step({'mode': 'rank', 'dimension': 'product', 'metric': 'totalWeight', 'stage': 'black', 'limit': 100}, task, {'releaseId': RELEASE, 'scopes': []})
self.assertEqual(args['limit'], 100)
with self.assertRaises(QualityToolError): prepare_step({**args, 'limit': 101}, task, {'releaseId': RELEASE, 'scopes': []})
def test_model_clarification_cannot_enter_native_execution(self):
value = task_value(executionMode='native_agent', needsClarification=True,
clarification='重量筛选用黑片、亮片、任一阶段还是两阶段都满足?', clarificationRequest=['黑片','亮片','任一阶段','两阶段均满足'], period={'year': 2026, 'month': 10}, dimensions=['product'])
context = {'_protocol': {'autonomousEnabled': True}, '_businessCatalog': {'years': [2026]}}
with patch('nao_agent_task._model_json', return_value=value):
task = plan_task(base_url='http://test', api_key='test', model='test', question=QUESTION,
context=context, cancel_event=threading.Event(), checks_enabled=False)
self.assertTrue(task['needsClarification'])
self.assertNotEqual(task.get('executionMode'), 'native_agent')
self.assertEqual(task['clarification'], value['clarification'])
self.assertEqual([entry['value'] for entry in task['clarificationRequest']['options']], value['clarificationRequest'])
def test_unrelated_new_tasks_and_additional_ambiguity_are_not_swallowed(self):
previous = root(); previous.update(needsClarification=True, aggregateSelection=selection(stage=None))
context = {'_agentTask': previous}
self.assertIsNone(selection_hint('新任务,查询综合率', context))
value = task_value(needsClarification=True, clarification='产品有多个候选,请选择具体规格。')
fixed = canonical_proposal(value, question='亮片', context=context)
self.assertTrue(fixed['needsClarification'])
self.assertEqual(fixed['clarification'], value['clarification'])
self.assertEqual(selection_hint('改成Top100,亮片', context)['limit'], 100)
def test_unpaginated_metrics_do_not_require_pagination_only_rowcount(self):
raw = {'summary': {'key':'overall','compositeRate': .95}, 'context': {},
'rows':[{'key':'P1','compositeRate':.9}], 'coverage': {}, 'trend':[]}
tools = object.__new__(QualityTools)
with patch.object(tools, '_metrics', return_value=raw) as read:
result = tools._query({'id': RELEASE}, {'year':2026,'month':10}, {}, 'product', 500)
self.assertEqual(result['rowCount'], 1)
self.assertFalse(result['rowsTruncated'])
self.assertIsNone(read.call_args.kwargs['row_limit'])
raw['context']['rowCount'] = 2
with patch.object(tools, '_metrics', return_value=raw):
with self.assertRaises(QualityToolError): tools._query({'id':RELEASE}, {'year':2026,'month':10}, {}, 'product', 500)
def test_product_batch_charts_accept_only_exact_signed_dimension_scope(self):
from analysis_visual_evidence import protocol_visual_result
value = population()
value['protocolEvidence'] = {'nodeId':'products', 'scopeId':'scope-products','queryId':'q-products'}
document = {'mode':'query','releaseId':RELEASE,'result':value,'evidenceRef':'Q12345678'}
task = root(); task.update(period=value['period'], comparisonPeriod=None, comparisonRequired=False,
aggregateSelection=selection(100), dimensions=['product'], fields=['rate','totalWeight'], stages=['black','bright'])
scope = {'nodeId':'products','scopeId':'scope-products','purpose':'metrics','task':{**task,'kind':'metrics'}}
result = protocol_visual_result([document],[scope],QUESTION,'test-turn',release_id=RELEASE,base_context={'_agentTask':task})
self.assertEqual(result['visualState'], 'ready')
self.assertEqual(sum(len(chart['categories']) for chart in result['visuals']), 100)
changed = copy.deepcopy(document); changed['result']['protocolEvidence']['scopeId'] = 'wrong-scope'
denied = protocol_visual_result([changed],[scope],QUESTION,'test-turn',release_id=RELEASE,base_context={'_agentTask':task})
self.assertEqual(denied['visuals'], [])
empty=copy.deepcopy(task);empty['aggregateSelection']['weightFilter']['value']=1e12
no_chart=protocol_visual_result([document],[scope],QUESTION,'test-turn',release_id=RELEASE,base_context={'_agentTask':empty})
self.assertEqual(no_chart['visualState'],'not_requested')
def test_initial_top_comparison_read_is_never_a_single_period_weight_rank(self):
import runpy
initial = runpy.run_path(str(ROOT/'runtime/dsh/application/quality_native_run.py'))['initial_read_arguments']
task = root();task.update(freshQuery=True, dimensions=['product'], fields=['rate','totalWeight'], stages=['bright'])
self.assertEqual(initial(task,'合格率下降最多Top100产品'), {'mode':'compare'})
task['aggregateSelection']=selection(100)
self.assertEqual(initial(task,'合格率下降最多Top100产品'), {'mode':'compare'})
def test_two_period_rate_change_uses_existing_changes_contract(self):
question='比较2026年9月和10月,列出亮片合格率下降最多的产品Top100'
value=task_value(fields=['rate'],stages=['bright'],dimensions=['product'],
aggregateSelection={'dimension':'product','metric':'rate','rateStage':'bright','direction':'asc','limit':100,'requestQuestion':question})
fixed=canonical_proposal(value,question=question)
self.assertNotIn('aggregateSelection',fixed)
self.assertEqual(fixed['period'],{'year':2026,'month':10})
self.assertEqual(fixed['comparisonPeriod'],{'year':2026,'month':9})
if __name__ == '__main__': unittest.main()