修复:DSH 自主查询、图片直显与报告稳定性
允许 Agent 在循环中查目录、修正查询和提问,保留实时权限检查及模型原始回答。 统一相同查询证据与图表资源;排名补齐阶段指标,修正 OR 重量筛选,并限制模型明细输入。 增加运行恢复、图表自适应坐标轴与两位小数,修复报告主图选择、重复图及边缘标签。 验证:独立副本通过 162 项 Python、73 项前端及 21 项后端测试,前端构建通过。 线上已用原 8 题复测,并追加两轮双期对比;TOP100 与组合筛选分别核对 100 行和 20 行。
This commit is contained in:
@@ -890,6 +890,15 @@ class AnalysisVisualsTest(unittest.TestCase):
|
||||
self.assertEqual([chart["metric"] for chart in charts], ["composite_rate"])
|
||||
self.assertEqual(charts[0]["series"][0]["values"], [0.91, None, 0.93, 0.94, None])
|
||||
|
||||
def test_requeried_evidence_keeps_image_address_but_changed_data_does_not(self):
|
||||
first = build_visuals([evidence(comparison_result(), ref='Q1234ABCD')], '比较两个期间变化', 'turn-stable')['visuals'][0]
|
||||
second = build_visuals([evidence(comparison_result(), ref='Q9876ABCD')], '比较两个期间变化', 'turn-stable')['visuals'][0]
|
||||
self.assertEqual(first['id'], second['id'])
|
||||
self.assertNotEqual(first['evidenceRefs'], second['evidenceRefs'])
|
||||
changed = comparison_result(); changed['current']['compositeRate'] = .97
|
||||
third = build_visuals([evidence(changed)], '比较两个期间变化', 'turn-stable')['visuals'][0]
|
||||
self.assertNotEqual(first['id'], third['id'])
|
||||
|
||||
def test_ids_are_stable_per_semantics_and_output_is_isolated(self):
|
||||
doc = evidence(comparison_result())
|
||||
doc["absoluteEvidencePath"] = "/Users/alice/private/source.json"
|
||||
|
||||
@@ -22,7 +22,7 @@ from setup_dsh_runtime import inspect_runtime, runtime_node
|
||||
|
||||
class AuthoredModel:
|
||||
"""A deterministic provider wire fixture which exercises the actual DSH loop."""
|
||||
def __init__(self, *, stall=False, length=False, transient=False, malformed=False):
|
||||
def __init__(self, *, stall=False, length=False, transient=False, malformed=False, repair=False):
|
||||
self.calls, self.stall, self.length = [], stall, length
|
||||
self.started = threading.Event()
|
||||
self.release = threading.Event()
|
||||
@@ -60,10 +60,12 @@ class AuthoredModel:
|
||||
if owner.stall:
|
||||
owner.release.wait(20)
|
||||
return
|
||||
if not any(message.get('role') == 'tool' for message in body['messages']):
|
||||
send({'tool_calls': [{'index': 0, 'id': 'authored-quality-query', 'type': 'function',
|
||||
if not any(message.get('role') == 'tool' for message in body['messages']) or repair and len(owner.calls)<3:
|
||||
arguments = ('{"mode":"query","period":{"year":2026},"filters":{"product":"'
|
||||
+ ('18X15X4' if len(owner.calls)==1 else '18X15X4+T') + '"}}') if repair else '{"mode":"catalog"}'
|
||||
send({'tool_calls': [{'index': 0, 'id': 'authored-quality-query-'+str(len(owner.calls)), 'type': 'function',
|
||||
'function': {'name': runtime.PUBLIC_TOOL, 'arguments':
|
||||
'{"mode":' if malformed and len(owner.calls) == 1 else '{"mode":"catalog"}'}}]})
|
||||
'{"mode":' if malformed and len(owner.calls) == 1 else arguments}}]})
|
||||
send({}, 'tool_calls')
|
||||
else:
|
||||
send({'content': '已核实'})
|
||||
@@ -263,6 +265,20 @@ class RealDshTests(unittest.TestCase):
|
||||
self.assertEqual(acc.tool_calls[0]['status'], 'failed')
|
||||
self.assertIn('当前范围未授权', acc.tool_calls[0]['outputSummary'])
|
||||
|
||||
def test_real_loop_corrects_a_rejected_product_argument_and_continues(self):
|
||||
from nao_quality_tools import QualityToolError
|
||||
seen=[]
|
||||
def query(arguments):
|
||||
seen.append(arguments['filters']['product'])
|
||||
if seen[-1]=='18X15X4':
|
||||
raise QualityToolError('名称未匹配,请使用完整规格18X15X4+T。',code='invalid_arguments')
|
||||
return {'kind':'quality_metrics','message':'完整规格查询成功'}
|
||||
acc,status,reason,stopped=self.run_agent(AuthoredModel(repair=True),tool=query)
|
||||
self.assertEqual((status,reason,stopped),('COMPLETED','stop',True),self.summary)
|
||||
self.assertEqual(seen,['18X15X4','18X15X4+T'])
|
||||
self.assertEqual([call['status'] for call in acc.tool_calls],['failed','completed'])
|
||||
self.assertEqual(acc.answer,'已核实质量指标。')
|
||||
|
||||
def test_no_data_is_a_normal_tool_outcome_without_zero_metrics(self):
|
||||
acc,status,reason,stopped=self.run_agent(AuthoredModel(),tool=lambda args:{
|
||||
'kind':'quality_no_data','status':'no_data','summary':None,'rows':[],
|
||||
|
||||
@@ -61,12 +61,13 @@ class DshApplicationTests(unittest.TestCase):
|
||||
self.assertEqual(instance.access_path.stat().st_mode & 0o777, 0o600)
|
||||
access_files.append(instance.access_path)
|
||||
if path == '/api/analysis/protocol/task':
|
||||
self.assertEqual(kwargs['payload'], task)
|
||||
return {'task': task, 'attemptId': request['sessionId']}
|
||||
self.assertTrue(kwargs['payload']['agentDriven'])
|
||||
self.assertEqual(kwargs['payload']['filters'], {})
|
||||
return {'task': kwargs['payload'], 'attemptId': request['sessionId']}
|
||||
return {}
|
||||
|
||||
def execute(**options):
|
||||
self.assertEqual(options['authority']['task'], task)
|
||||
self.assertTrue(options['authority']['task']['agentDriven'])
|
||||
self.assertNotIn('accessToken', options['request'])
|
||||
self.assertEqual(options['connection'][0].split(':')[1], '//127.0.0.1')
|
||||
self.original.current().capture_native(answer)
|
||||
@@ -94,6 +95,33 @@ class DshApplicationTests(unittest.TestCase):
|
||||
self.assertEqual(result['timeline'], [{'kind': 'tool_result', 'toolCallId': 'dsh-tool-fixture'}])
|
||||
self.assertNotIn('fixture-only-delegation', output.getvalue())
|
||||
|
||||
def test_intent_errors_start_dsh_but_real_authorization_refusal_stops(self):
|
||||
for error,should_start in [(ValueError('参数格式待修正'),True),
|
||||
(self.context.RuntimeErrorSafe('查询条件待修正',http_status=409),True),
|
||||
(self.context.RuntimeErrorSafe('当前账号无权限',http_status=403),False)]:
|
||||
with self.subTest(error=type(error).__name__,start=should_start):
|
||||
request=self.request('18x15x4今年合格率变化趋势')
|
||||
started=[];output=io.StringIO()
|
||||
def delegate(instance,path,**kwargs):
|
||||
if path=='/api/analysis/protocol/task':
|
||||
return {'task':kwargs['payload'],'attemptId':request['sessionId']}
|
||||
return {}
|
||||
def execute(**options):
|
||||
started.append(True)
|
||||
answer='找到多个完整规格,请确认要分析哪一个。'
|
||||
self.original.current().capture_native(answer)
|
||||
options['emit']({'type':'result','status':'COMPLETED','answer':answer,'agentTask':options['task']})
|
||||
with (patch('setup_dsh_runtime.inspect_runtime',return_value={'ready':True}),
|
||||
patch('quality_model_settings.model_configuration',return_value=('https://fixture.invalid/v1','fixture-key','fixture-model',{})),
|
||||
patch.object(self.context.DelegatedApi,'call',delegate),
|
||||
patch.object(self.worker,'_plan',side_effect=error),
|
||||
patch('quality_native_run.run_native_exploration',side_effect=execute)):
|
||||
self.worker.run_turn(request,stdin=io.StringIO(),stdout=output)
|
||||
self.assertEqual(bool(started),should_start)
|
||||
result=[json.loads(line) for line in output.getvalue().splitlines() if json.loads(line).get('type')=='result'][-1]
|
||||
self.assertEqual(result['status'],'COMPLETED' if should_start else 'FAILED')
|
||||
if should_start:self.assertEqual(result['modelAnswer'],result['answer'])
|
||||
|
||||
def test_engine_and_attempt_are_pinned_before_access(self):
|
||||
requests = (self.request('检查指标', runtimeEngine='legacy'),
|
||||
self.request('检查指标', context={'_protocol': {'version': 1, 'attemptId': str(uuid.uuid4())}}))
|
||||
@@ -249,6 +277,41 @@ class DshApplicationTests(unittest.TestCase):
|
||||
self.assertEqual(result['phaseTimingsMs']['synthesis'], 0)
|
||||
self.assertNotIn('answerQuality', result)
|
||||
|
||||
def test_query_exposes_the_same_real_chart_resource_that_final_delivery_contains(self):
|
||||
runner = importlib.import_module('quality_native_run')
|
||||
task = {'kind': 'analysis', 'agentDriven': True, 'executionMode': 'native_agent',
|
||||
'filters': {}, 'period': {'year': 2026}, 'fields': ['rate'], 'stages': []}
|
||||
request = self.request('今年合格率趋势,用图片直接展示')
|
||||
authority = {**{key: request[key] for key in ('sessionId', 'turnId')},
|
||||
'attemptId': request['sessionId'], 'revision': 1, 'executionId': str(uuid.uuid4())}
|
||||
visual = {'id': 'real-chart-id', 'title': '真实趋势图', 'scope': {'product': '完整规格'}, 'period': {'year': 2026}}
|
||||
answer = '原始结论。\n\n'
|
||||
accumulator = SimpleNamespace(answer=answer, finish_reasons=['stop'], tool_calls=[], timeline=[])
|
||||
observations = []
|
||||
def execute(**options):
|
||||
observations.append(options['tool_handler']({'mode': 'query'}))
|
||||
observations.append(options['tool_handler']({'mode': 'query', 'freshQuery': True}))
|
||||
return accumulator, 'COMPLETED', 'stop', True
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory); (root/'private').mkdir(); (root/'evidence').mkdir()
|
||||
with (patch('dsh_agent_runtime.run_dsh_agent', side_effect=execute),
|
||||
patch('quality_native_exploration.NativeExplorationTools.quality_data', side_effect=[{'value': 0.9639601707533932}, {'value': 0.9639601707533932, 'reusedEvidence': True}]),
|
||||
patch('quality_native_exploration.native_model_view', side_effect=lambda value, **kwargs: value),
|
||||
patch('analysis_visual_evidence.protocol_visual_result', return_value={'visualSchemaVersion': 1, 'visualState': 'ready', 'visuals': [visual]})):
|
||||
result = runner.run_native_exploration(request=request, task=task, authority=authority,
|
||||
actor_root=root, delegated=SimpleNamespace(call=lambda *args, **kwargs: {}),
|
||||
connection=('http://127.0.0.1:1/v1', 'fixture', 'fixture'),
|
||||
cancel_event=threading.Event(), emit=lambda event: None, prior_evidence_files=frozenset())
|
||||
image = observations[0]['presentation']['images'][0]
|
||||
self.assertEqual(image['src'], 'chart:' + result['visuals'][0]['id'])
|
||||
self.assertEqual(observations[0]['value'], 0.9639601707533932)
|
||||
self.assertEqual(observations[0]['queryCompletion']['state'], 'data_ready')
|
||||
self.assertTrue(observations[0]['queryCompletion']['newEvidence'])
|
||||
self.assertTrue(observations[1]['queryCompletion']['reused'])
|
||||
self.assertFalse(observations[1]['queryCompletion']['newEvidence'])
|
||||
self.assertEqual(observations[0]['presentation']['images'], observations[1]['presentation']['images'])
|
||||
self.assertEqual(result['answer'], answer)
|
||||
|
||||
def test_business_data_is_read_before_model_and_seeded_in_the_native_stream(self):
|
||||
runner = importlib.import_module('quality_native_run')
|
||||
task = {'kind': 'analysis', 'freshQuery': True, 'executionMode': 'native_agent', 'filters': {},
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import json
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
|
||||
from dsh_startup import execution_task
|
||||
from nao_native_exploration import prepare_step, NativeExplorationTools
|
||||
from nao_quality_tools import QualityToolError, tool_definition
|
||||
|
||||
|
||||
class AutonomousStartTests(unittest.TestCase):
|
||||
def task(self, **extra):
|
||||
return execution_task(extra, '18x15x4今年合格率变化趋势', {'periods':['2026-09','2026-10']})
|
||||
|
||||
def test_unresolved_product_and_invalid_hints_do_not_block_start(self):
|
||||
task=self.task(needsClarification=True, filters={'product':'18X15X4'}, fields=['unknown'],
|
||||
period={'year':2026,'month':99}, queryPlan=[{}])
|
||||
self.assertTrue(task['agentDriven']); self.assertFalse(task['needsClarification'])
|
||||
self.assertEqual(task['filters'], {}); self.assertEqual(task['period'], {'year':2026})
|
||||
self.assertEqual(task['fields'], ['rate']); self.assertNotIn('queryPlan',task)
|
||||
|
||||
def test_query_can_correct_product_period_and_multiple_conditions_without_parent_read(self):
|
||||
task=self.task(period={'year':2026,'month':9}, filters={'product':'wrong'})
|
||||
state={'releaseId':'r','scopes':[]}
|
||||
args,proposal=prepare_step({'mode':'query','period':{'year':2026,'month':10},
|
||||
'filters':{'product':'18X15X4+T','department':'工业电机产业部'},'fields':['rate']},task,state)
|
||||
self.assertEqual(args['period'],{'year':2026,'month':10})
|
||||
self.assertEqual(proposal['frame']['filters'],args['filters'])
|
||||
self.assertEqual(proposal['parentQueryIds'],[])
|
||||
self.assertNotIn('fields',args)
|
||||
|
||||
def test_three_different_parameter_errors_still_allow_catalog_search(self):
|
||||
class Api:
|
||||
def call(self,path,**kwargs):
|
||||
if path=='/api/analysis/model-authorization':return {}
|
||||
if path.startswith('/api/options?'):return {'products':['18X15X4+T','18X15X4+Z']}
|
||||
raise AssertionError(path)
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
tools=NativeExplorationTools(delegated=Api(),evidence_dir=Path(directory)/'evidence',task=self.task(),
|
||||
identity={'executionId':'execution','releaseId':'r'},state_path=Path(directory)/'state.json')
|
||||
for stage in ('bad-a','bad-b','bad-c'):
|
||||
with self.assertRaises(QualityToolError):tools.quality_data({'mode':'rank','stage':stage,'dimension':'product','metric':'scrapWeight'})
|
||||
result=tools.quality_data({'mode':'catalog','dimension':'product','search':'18x15x4'})
|
||||
self.assertEqual([item['name'] for item in result['items']],['18X15X4+T','18X15X4+Z'])
|
||||
self.assertFalse(json.loads((Path(directory)/'state.json').read_text()).get('halted'))
|
||||
|
||||
def test_schema_exposes_search_and_recovery_modes_without_planning_permission(self):
|
||||
schema=tool_definition(native_protocol=True,task=self.task())['inputSchema']
|
||||
self.assertIn('search',schema['properties'])
|
||||
self.assertIn('fields',schema['properties'])
|
||||
self.assertTrue({'catalog','query','rank','compare','changes','defects'}<=set(schema['properties']['mode']['enum']))
|
||||
self.assertNotIn('const',schema['properties']['period'])
|
||||
self.assertIn('comparisonPeriod',schema['properties'])
|
||||
|
||||
|
||||
if __name__=='__main__':unittest.main()
|
||||
@@ -10,6 +10,17 @@ from nao_weight_ranking import rank_response
|
||||
|
||||
|
||||
class DshModelInputTests(unittest.TestCase):
|
||||
def test_history_image_projection_preserves_stored_answers_and_user_questions(self):
|
||||
from dsh_model_input import presentation_context
|
||||
source = {'_serverHistory': [{'answer': '结论。',
|
||||
'question': '解释 '}]}
|
||||
before = copy.deepcopy(source)
|
||||
result = presentation_context(source)
|
||||
self.assertEqual(source, before)
|
||||
self.assertEqual(result['_serverHistory'][0]['answer'], '结论。真实标题')
|
||||
self.assertEqual(result['_serverHistory'][0]['question'], source['_serverHistory'][0]['question'])
|
||||
self.assertEqual(result['presentation']['imageResources'], [])
|
||||
|
||||
def test_recent_monthly_trend_binds_published_months_instead_of_one_month(self):
|
||||
context = {'_businessCatalog': {'periods': [f'2026-{month:02d}' for month in range(1, 11)],
|
||||
'latestDataPeriod': {'year': 2026, 'month': 10}}}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import copy,json,sys,unittest,threading
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'scripts'))
|
||||
from dsh_row_selection import selection_hint,select_rows
|
||||
from dsh_model_input import compact_tool_result,canonical_proposal
|
||||
from nao_native_exploration import prepare_step
|
||||
from nao_weight_ranking import rank_response
|
||||
|
||||
class MultiTypeRegressions(unittest.TestCase):
|
||||
def test_or_weight_condition_is_bound_before_top_selection(self):
|
||||
for relation,stage in [('任一','any'),('均','all'),('都','all')]:
|
||||
q='筛选2026年10月综合合格率最低的TOP20产品,要求黑片投入重量或亮片投入重量'+relation+'大于1000kg'
|
||||
selection=selection_hint(q)
|
||||
self.assertEqual(selection.get('weightFilter'),{'metric':'totalWeight','stage':stage,'operator':'gt','value':1000})
|
||||
rows=[{'key':'small','compositeRate':0,'black':{'totalWeight':10},'bright':{'totalWeight':1}}, {'key':'one','compositeRate':.9,'black':{'totalWeight':2000},'bright':{'totalWeight':1}}, {'key':'both','compositeRate':.95,'black':{'totalWeight':2000},'bright':{'totalWeight':1500}}]
|
||||
selected=select_rows({'kind':'quality_metrics','view':'product','rows':rows,'rowCount':3,'rowsTruncated':False},selection)
|
||||
self.assertEqual([r['key'] for r in selected['rows']],['one','both'] if stage=='any' else ['both'])
|
||||
def test_rank_carries_stage_rate_and_weights_from_same_response(self):
|
||||
source={'rows':[{'key':'p','black':{'rate':.91,'scrapWeight':9,'totalWeight':100}}], 'summary':{'black':{'scrapWeight':9}},'coverage':{}}
|
||||
result=rank_response(source,{'id':'r'},{'year':2026,'month':10},{},'product','black','scrapWeight',100,include_metrics=True)
|
||||
self.assertEqual(result['rows'][0]['metrics'],{'rate':.91,'scrapWeight':9,'totalWeight':100})
|
||||
def test_group_result_input_is_bounded_without_modifying_evidence(self):
|
||||
row={'key':'p','compositeRate':.91,'black':{'rate':.95,'totalWeight':100,'scrapWeight':5,'defects':[{'name':'x','weight':1}]*40},'bright':{'rate':.96,'totalWeight':100,'scrapWeight':4}}
|
||||
source={'kind':'quality_metrics','view':'product','rows':[dict(copy.deepcopy(row),key=str(i)) for i in range(2667)],'rowCount':2667,'rowsTruncated':False,'evidenceRef':'Q12345678'}
|
||||
before=copy.deepcopy(source)
|
||||
first=compact_tool_result(source,task={})
|
||||
second=compact_tool_result(source,task={'modelPage':{'offset':100,'limit':100}})
|
||||
self.assertEqual(source,before)
|
||||
self.assertEqual(len(first['rows']),100)
|
||||
self.assertLess(len(json.dumps(first)),80000)
|
||||
self.assertTrue(first['modelPage']['hasMore']);self.assertTrue(first['rowsTruncated'])
|
||||
self.assertEqual(second['rows'][0]['key'],'100');self.assertEqual(first['rows'][0]['black']['rate'],.95)
|
||||
def test_overall_quality_report_cannot_lose_rate_from_model_input(self):
|
||||
value=canonical_proposal({'kind':'analysis','fields':['totalWeight','scrapWeight'],'stages':['black','bright'],'trendRequired':True},question='生成2026年1—10月总体质量分析报告,重点展示月度趋势和黑片亮片报废结构')
|
||||
self.assertIn('rate',value['fields'])
|
||||
def test_query_comparison_is_normalized_before_validation(self):
|
||||
task={'agentDriven':True,'period':{'year':2026,'month':10},'fields':['rate'],'stages':[],'filters':{}}
|
||||
args,proposal=prepare_step({'mode':'query','period':{'year':2026,'month':10},'comparisonPeriod':{'year':2026,'month':9},'fields':['rate']},task,{'releaseId':'r','scopes':[]})
|
||||
self.assertEqual(args['mode'],'compare');self.assertTrue(proposal['node']['comparisonRequired'])
|
||||
def test_transport_recovery_reuses_successful_results_and_preserves_final_text(self):
|
||||
import dsh_agent_runtime as runtime
|
||||
failed=(object(),'FAILED','transport_or_stream_error',True);final=(object(),'COMPLETED','stop',True)
|
||||
def execute(**kwargs):
|
||||
if not kwargs['context'].get('runtimeRecovery'):
|
||||
kwargs['tool_handler']({'mode':'query'});return failed
|
||||
self.assertEqual(kwargs['context']['runtimeRecovery']['observations'][0]['result']['summary']['compositeRate'],.95)
|
||||
return final
|
||||
with patch.object(runtime,'_execute',side_effect=execute) as run:
|
||||
result=runtime.run_dsh_agent(actor_root=Path('/unused'),connection=(),question='原问题',context={},task={},identity={},turn_id='t',cancel_event=threading.Event(),emit=lambda e:None,tool_handler=lambda a:{'kind':'quality_metrics','summary':{'compositeRate':.95}},authorize=lambda:None,tool_definition={})
|
||||
self.assertIs(result,final);self.assertEqual(run.call_count,2)
|
||||
|
||||
def test_recovery_does_not_restart_after_permission_denial_or_user_cancel(self):
|
||||
import dsh_agent_runtime as runtime
|
||||
for reason in ('authorization_rejected','user_cancel','timeout'):
|
||||
outcome=(object(),'FAILED',reason,True)
|
||||
with patch.object(runtime,'_execute',return_value=outcome) as run:
|
||||
self.assertIs(runtime.run_dsh_agent(actor_root=Path('/unused'),connection=(),question='q',context={},task={},identity={},turn_id='t',cancel_event=threading.Event(),emit=lambda e:None,tool_handler=lambda a:{},authorize=lambda:None,tool_definition={}),outcome)
|
||||
self.assertEqual(run.call_count,1)
|
||||
def test_startup_is_nonblocking_and_keeps_literal_or_and_report_rate_hints(self):
|
||||
from dsh_startup import execution_task
|
||||
q='筛选综合合格率最低TOP20产品,黑片投入重量或亮片投入重量任一大于1000kg'
|
||||
task=execution_task({},q,{'periods':['2026-10']})
|
||||
self.assertTrue(task['agentDriven']);self.assertEqual(task['aggregateSelection']['weightFilter']['stage'],'any')
|
||||
report=execution_task({'fields':['totalWeight','scrapWeight']},'生成2026年1—10月总体质量分析报告',{'periods':['2026-10']})
|
||||
self.assertIn('rate',report['fields'])
|
||||
|
||||
def test_rank_auxiliary_columns_do_not_create_additional_ranking_obligations(self):
|
||||
task={'agentDriven':True,'period':{'year':2026,'month':10},'fields':['rate','scrapWeight'],'stages':['black'],'filters':{}}
|
||||
args,proposal=prepare_step({'mode':'rank','dimension':'product','stage':'black','metric':'scrapWeight','limit':100},task,{'releaseId':'r','scopes':[]})
|
||||
self.assertEqual(proposal['node']['fields'],['scrapWeight'])
|
||||
|
||||
def test_rate_comparison_projection_contains_only_needed_values_and_keeps_evidence_unchanged(self):
|
||||
point={'period':{'year':2026,'month':10},'compositeRate':.95,'black':{'rate':.97,'totalWeight':500,'defects':[{'name':'x','weight':1}]*40},'bright':{'rate':.98},'calculationRules':{'verbose':'x'*10000}}
|
||||
source={'kind':'quality_comparison','current':point,'comparison':dict(copy.deepcopy(point),period={'year':2026,'month':9}),'delta':{'percentagePoints':.2},'stageRateChangesPP':{'black':.1,'bright':.1},'filterBinding':{},'evidenceRef':'Q12345678'}
|
||||
original=copy.deepcopy(source);view=compact_tool_result(source,task={'fields':['rate'],'dimensions':[]})
|
||||
self.assertEqual(source,original);self.assertLess(len(json.dumps(view)),4000)
|
||||
self.assertEqual(view['current']['black'],{'rate':.97})
|
||||
self.assertEqual(view['comparisonAnswer']['rows'][0]['current'],.95)
|
||||
@@ -645,6 +645,47 @@ class NativeExplorationTests(unittest.TestCase):
|
||||
with self.assertRaisesRegex(QualityToolError, '停止'):
|
||||
tools.quality_data({'mode': 'compare'})
|
||||
|
||||
def test_agent_same_query_refresh_and_default_view_reuse_without_stopping(self):
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
api = Delegated(); p = Path(folder); task = {**root(), 'agentDriven': True}
|
||||
tools = NativeExplorationTools(delegated=api, evidence_dir=p/'evidence', task=task,
|
||||
identity={**identity(), 'releaseId': RELEASE}, state_path=p/'state.json')
|
||||
first = tools.quality_data({'mode': 'compare'})
|
||||
for _ in range(5):
|
||||
duplicate = tools.quality_data({'mode': 'compare', 'freshQuery': True})
|
||||
self.assertTrue(duplicate.get('reusedEvidence'))
|
||||
self.assertEqual(duplicate['evidenceRef'], first['evidenceRef'])
|
||||
before = len(api.steps)
|
||||
tools.quality_data({'mode': 'query'})
|
||||
duplicate = tools.quality_data({'mode': 'query', 'view': 'overall', 'freshQuery': True})
|
||||
self.assertTrue(duplicate.get('reusedEvidence'))
|
||||
self.assertEqual(len(api.steps), before + 1)
|
||||
self.assertEqual(api.authorization_calls, 8)
|
||||
self.assertFalse(json.loads((p/'state.json').read_text()).get('halted', False))
|
||||
|
||||
def test_agent_cached_result_is_not_returned_after_authorization_revoked(self):
|
||||
from nao_quality_runtime import RuntimeErrorSafe
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
api = Delegated(); p = Path(folder); task = {**root(), 'agentDriven': True}
|
||||
tools = NativeExplorationTools(delegated=api, evidence_dir=p/'evidence', task=task,
|
||||
identity={**identity(), 'releaseId': RELEASE}, state_path=p/'state.json')
|
||||
tools.quality_data({'mode': 'compare'})
|
||||
with patch.object(api, 'call', side_effect=RuntimeErrorSafe('权限已撤回', http_status=403)):
|
||||
with self.assertRaisesRegex(RuntimeErrorSafe, '权限已撤回'):
|
||||
tools.quality_data({'mode': 'compare', 'freshQuery': True})
|
||||
|
||||
def test_agent_different_metrics_do_not_alias_same_query_cache(self):
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
api = Delegated(); p = Path(folder); task = {**root(), 'agentDriven': True}
|
||||
tools = NativeExplorationTools(delegated=api, evidence_dir=p/'evidence', task=task,
|
||||
identity={**identity(), 'releaseId': RELEASE}, state_path=p/'state.json')
|
||||
tools.quality_data({'mode': 'query', 'fields': ['rate']})
|
||||
second = tools.quality_data({'mode': 'query', 'fields': ['scrapWeight']})
|
||||
self.assertFalse(second.get('reusedEvidence', False))
|
||||
self.assertEqual(len(api.steps), 2)
|
||||
schema = tool_definition(native_protocol=True, task=task, release_id=RELEASE)['inputSchema']
|
||||
self.assertNotIn('freshQuery', schema['properties'])
|
||||
|
||||
def test_new_execution_never_reuses_previous_attempt_cache(self):
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
api = Delegated(); p = Path(folder)
|
||||
|
||||
Reference in New Issue
Block a user