"""A saved clarification selection survives only authorized fresh retries.""" import io import json import copy import sys import tempfile import threading import unittest from contextlib import ExitStack from pathlib import Path from unittest.mock import MagicMock, patch sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts')) import nao_quality_runtime as runtime from nao_product_identification import identify_products, resolve_clarification_choice ACTOR = '11111111-1111-4111-8111-111111111111' SESSION = '22222222-2222-4222-8222-222222222222' RELEASE = '33333333-3333-4333-8333-333333333333' TURN = '55555555-5555-4555-8555-555555555555' class ProductClarificationRuntimeTests(unittest.TestCase): def test_catalog_prefix_with_multiple_candidates_forces_explicit_choice(self): question = ('查询2026年10月合成产品0的黑片合格率;如果名称不能唯一确定,' '请让我明确选择,不要自行猜产品。') authorized = [f'合成产品0{i}' for i in range(1, 7)] options = {'products': authorized, 'organizationPaths': []} match = identify_products(question, options)['productMatch'] self.assertEqual(match['status'], 'ambiguous') self.assertEqual(match['query'], '合成产品0') self.assertEqual(match['candidateCount'], 6) self.assertFalse(match['truncated']) self.assertEqual([item['name'] for item in match['candidates']], authorized) # Even if the planner mistakenly forwards the partial name as a filter # and omits needsClarification, the deterministic authorized catalog # overrides it while preserving the user's metric and scope. from nao_agent_task import plan_task planned = { 'goal': question, 'kind': 'metrics', 'freshQuery': True, 'dimensions': [], 'dimensionStages': {}, 'stages': ['black'], 'fields': ['rate'], 'period': {'year': 2026, 'month': 10}, 'comparisonRequired': False, 'trendRequired': False, 'verifySource': False, 'fullDetails': False, 'visualPreference': 'none', 'responseStyle': 'standard', 'needsClarification': False, 'clarification': '', 'filterUpdates': {'product': '合成产品0'}, } context = {'_protocol': {'version': 1, 'autonomousEnabled': True}, '_businessCatalog': {'productMatch': match}} with patch('nao_agent_task._model_json', return_value=planned): pending = plan_task(base_url='https://offline.invalid/v1', api_key='synthetic', model='m', question=question, context=context, cancel_event=threading.Event()) self.assertTrue(pending['needsClarification']) self.assertNotIn('product', pending['filters']) self.assertEqual(pending['period'], {'year': 2026, 'month': 10}) self.assertEqual(pending['fields'], ['rate']) self.assertEqual(pending['stages'], ['black']) self.assertEqual([item['value'] for item in pending['clarificationRequest']['options']], authorized) selected = resolve_clarification_choice('选择合成产品03', options, pending) self.assertEqual(selected['productMatch']['status'], 'exact') self.assertTrue(selected['productMatch']['selectedFromClarification']) self.assertEqual(selected['productMatch']['candidates'][0]['name'], '合成产品03') def test_prefix_matching_stays_with_current_authorized_catalog_and_candidate_cap(self): question = '查询合成产品0的黑片合格率' names = [f'合成产品0{i:02d}' for i in range(25)] match = identify_products(question, {'products': names})['productMatch'] self.assertEqual(match['status'], 'ambiguous') self.assertEqual(match['candidateCount'], 25) self.assertEqual(len(match['candidates']), 20) self.assertTrue(match['truncated']) self.assertEqual(set(item['name'] for item in match['candidates']), set(names[:20])) not_authorized = identify_products('查询合成产品99的黑片合格率', {'products': names})['productMatch'] self.assertEqual(not_authorized['status'], 'none') self.assertEqual(not_authorized['candidates'], []) unique = identify_products('查询合成产品0的黑片合格率', {'products': ['合成产品01']})['productMatch'] self.assertEqual(unique['status'], 'unique') self.assertEqual([item['name'] for item in unique['candidates']], ['合成产品01']) # A unique partial prefix is bound to the full authorized directory # name, never sent to the API as a partial product filter. from nao_agent_task import plan_task question = '查询合成产品0的黑片合格率' planned = { 'goal': question, 'kind': 'metrics', 'freshQuery': True, 'dimensions': [], 'dimensionStages': {}, 'stages': ['black'], 'fields': ['rate'], 'period': {'year': 2026, 'month': 10}, 'comparisonRequired': False, 'trendRequired': False, 'verifySource': False, 'fullDetails': False, 'visualPreference': 'none', 'responseStyle': 'standard', 'needsClarification': False, 'clarification': '', 'filterUpdates': {'product': '合成产品0'}, } context = {'_protocol': {'version': 1, 'autonomousEnabled': True}, '_businessCatalog': {'productMatch': unique}} with patch('nao_agent_task._model_json', return_value=planned): task = plan_task(base_url='https://offline.invalid/v1', api_key='synthetic', model='m', question=question, context=context, cancel_event=threading.Event()) self.assertFalse(task['needsClarification']) self.assertEqual(task['filters']['product'], '合成产品01') def exercise(self, *, restart=False, revoke=False, other_release=False): opts = {'products': ['18X15X4+T', '18X15X4+Z'], 'organizationPaths': []} pending_match = identify_products('今年18x15x4合格率变化趋势', opts)['productMatch'] previous = {'kind':'analysis', 'question':'选+T', 'goal':'今年18x15x4合格率变化趋势', 'rootGoal':'今年18x15x4合格率变化趋势', 'period':{'year':2026}, 'filters':{'product':'18X15X4+T'}, 'stages':[], 'needsClarification':False, 'productResolution':{'originalQuestion':'今年18x15x4合格率变化趋势', 'productMatch':pending_match,'selectedName':'18X15X4+T', 'selectionQuestion':'选+T'}} if revoke: opts['products'] = ['18X15X4+Z', '28X20X3+T'] context = {'releaseId':RELEASE, '_serverQuestions':[ {'turnId':'old-hello', 'question':'你好', 'status':'COMPLETED'}, {'turnId':'failed-choice', 'question':'选+T', 'status':'FAILED'}], '_serverHistory':[{'turnId':'failed-choice','question':'选+T','answer':'错误旧答复'}]} if restart: context['_restartConversation'] = True payload = {'turnId':TURN,'sessionId':SESSION,'actorId':ACTOR,'releaseId':RELEASE, 'accessToken':'synthetic-delegation','question':'选+T','context':context} planned = {'kind':'analysis','goal':'test','question':'选+T','filters':{},'stages':[], 'needsClarification':True,'clarification':'请选择规格','verifySource':False,'fullDetails':False} captured = {} with tempfile.TemporaryDirectory() as folder: root=Path(folder);(root/'private').mkdir();(root/'evidence').mkdir() saved=root/'private'/'last-agent-task.json' saved.write_text(json.dumps({'releaseId':('44444444-4444-4444-8444-444444444444' if other_release else RELEASE), 'sessionId':SESSION,'baseMetricFrame':None,'task':previous}),encoding='utf8') saved.chmod(0o600) manager=MagicMock();manager.acquire_actor.return_value=(root,None) state=MagicMock();state.get_chat_id.return_value=None gate=MagicMock();gate.base_url='http://127.0.0.1:1/v1' api=MagicMock() api.call.side_effect=lambda path,**kwargs: (opts if path.startswith('/api/options') else {'kind':'analysis_catalog','extraFields':[{'key':'customerCode','label':'客户代码'}]} if path=='/api/analysis/tools/catalog' else {}) delegated=MagicMock();delegated.login.return_value=api def plan(**kwargs): captured.update({k:copy.deepcopy(v) for k,v in kwargs.items() if k != 'cancel_event'}) return dict(planned) with ExitStack() as stack: for name,value in { 'RuntimeManager':MagicMock(return_value=manager), 'SessionState':MagicMock(return_value=state), 'write_access_file':MagicMock(return_value=root/'access.json'), '_input_cancel_listener':MagicMock(), '_model_settings':MagicMock(return_value=('https://offline.invalid/v1','key','model', {'enabled':False,'effort':'auto','protocol':'thinking'})), '_model_gate_settings':MagicMock(return_value=(1,'key')), 'DelegatedApi':MagicMock(return_value=delegated), 'plan_agent_task':plan, }.items(): stack.enter_context(patch.object(runtime,name,value)) stack.enter_context(patch('nao_model_gate.ModelGate',return_value=gate)) result=runtime.run_turn(payload,stdout=io.StringIO(),stdin=io.StringIO()) self.assertEqual(result['status'],'NEEDS_CLARIFICATION') return captured, previous def test_duplicate_failed_selection_keeps_goal_but_discards_failed_answer(self): captured, previous = self.exercise() self.assertEqual(captured['previous'], previous) self.assertTrue(captured['fresh_retry']) self.assertEqual(captured['context']['_serverHistory'], []) match = captured['context']['_businessCatalog']['productMatch'] self.assertTrue(match['selectedFromClarification']) self.assertEqual(match['candidates'][0]['name'], '18X15X4+T') self.assertEqual(match['previousQuestion'], '今年18x15x4合格率变化趋势') self.assertEqual(captured['context']['_businessCatalog']['extraFields'], [{'key':'customerCode','label':'客户代码'}]) def test_explicit_retry_also_restores_only_authorized_selection(self): captured, previous = self.exercise(restart=True) self.assertEqual(captured['previous'], previous) self.assertTrue(captured['fresh_retry']) self.assertEqual(captured['context']['_businessCatalog']['productMatch']['candidates'][0]['name'], '18X15X4+T') def test_revoked_or_different_release_never_restores_selection(self): for kwargs in ({'restart':True,'revoke':True},{'restart':True,'other_release':True}): with self.subTest(kwargs=kwargs): captured, _ = self.exercise(**kwargs) self.assertIsNone(captured['previous']) self.assertEqual(captured['context']['_businessCatalog']['productMatch']['status'], 'none') if __name__ == '__main__': unittest.main()