修复:智能分析空回答与工具结束条件冲突

统一自主分析工具范围与执行前校验,严格核验正常结束、最终正文和图表恢复状态,并显示空回答及恢复失败原因。

包含两次已上线修复及回归测试,保留已有回答与业务数据。

验证:1101 项 Python、469 项前端、97 项后端测试通过;前后端构建和真实模型、页面验收通过。
This commit is contained in:
2026-11-03 08:56:49 +08:00
parent 7ffb5cdb9c
commit 52d92c95d8
20 changed files with 387 additions and 37 deletions
+65
View File
@@ -114,6 +114,71 @@ class ModelGateTests(unittest.TestCase):
self.assertNotIn('fake-sensitive-provider-details', body)
self.assertEqual(len(self.calls), 1)
def test_unadvertised_tool_calls_are_blocked_before_json_or_stream_execution(self):
from unittest.mock import Mock
data_tool={'type':'function','function':{'name':'mcp_call','parameters':{'type':'object'}}}
tool={'id':'call-blocked','type':'function','function':{'name':'clarification','arguments':'{}'}}
reply={'choices':[{'index':0,'message':{'role':'assistant','content':None,'tool_calls':[tool]},'finish_reason':'tool_calls'}]}
upstream=HeldOpenProviderResponse([json.dumps(reply).encode()]);upstream.headers={'Content-Type':'application/json'};upstream.release.set()
self.gate._opener=Mock();self.gate._opener.open.return_value=upstream
code,body=self.request(extra={'tools':[data_tool]})
self.assertEqual(code,502);self.assertNotIn('clarification',body)
for newline in (True,False):
prefix=b'data: {"choices":[{"index":0,"delta":{"content":"Preserved text"}}]}\n\n'
bad=('data: '+json.dumps({'choices':[{'index':0,'delta':{'tool_calls':[tool]},'finish_reason':'tool_calls'}]})+ ('\n\n' if newline else '')).encode()
upstream=HeldOpenProviderResponse([prefix,bad[:19],bad[19:]]);upstream.release.set()
self.gate._opener.open.return_value=upstream
code,body=self.request(extra={'stream':True,'tools':[data_tool]})
self.assertEqual(code,200);self.assertIn('Preserved text',body)
self.assertNotIn('clarification',body);self.assertIn('error',body)
def test_allowed_fragmented_tool_calls_keep_their_exact_wire_data(self):
from unittest.mock import Mock
rows=[{'choices':[{'index':0,'delta':{'tool_calls':[{'index':0,'id':'q','type':'function','function':{'name':'mcp_','arguments':''}}]}}]},
{'choices':[{'index':0,'delta':{'tool_calls':[{'index':0,'function':{'name':'call','arguments':'{}'}}]},'finish_reason':'tool_calls'}]}]
wire=b''.join(('data: '+json.dumps(row)+'\n\n').encode() for row in rows)+b'data: [DONE]\n\n'
upstream=HeldOpenProviderResponse([wire[:23],wire[23:71],wire[71:]]);upstream.release.set()
self.gate._opener=Mock();self.gate._opener.open.return_value=upstream
code,body=self.request(extra={'stream':True,'tools':[{'type':'function','function':{'name':'mcp_call'}}]})
self.assertEqual(code,200);self.assertEqual(body.encode(),wire)
def test_follow_up_tool_cannot_end_analysis_before_the_model_writes_an_answer(self):
data_tool = {'type': 'function', 'function': {'name': 'mcp_call', 'parameters': {'type': 'object'}}}
follow_up = {'type': 'function', 'function': {'name': 'suggest_follow_ups', 'parameters': {'type': 'object'}}}
for choice in ('auto', {'type': 'function', 'function': {'name': 'suggest_follow_ups'}}):
with self.subTest(choice=choice):
self.assertEqual(self.request(extra={'tools': [data_tool, follow_up], 'tool_choice': choice})[0], 200)
self.assertEqual(self.calls[-1]['tools'], [data_tool])
if isinstance(choice, dict):
self.assertNotIn('tool_choice', self.calls[-1])
self.assertEqual(self.request(extra={'tools': [follow_up], 'tool_choice': 'required'})[0], 200)
self.assertNotIn('tools', self.calls[-1])
self.assertNotIn('tool_choice', self.calls[-1])
def test_only_supported_tools_reach_the_native_model_in_each_execution_mode(self):
helpers = ['read', 'list', 'grep', 'search', 'mcp_call', 'mcp_connect']
incompatible = ['clarification', 'request_warehouse_selection', 'request_warehouse_credentials',
'execute_sql', 'execute_semantic_query', 'read_query_result', 'display_chart', 'display_map',
'write', 'str_replace', 'execute_python', 'execute_sandboxed_code', 'task',
'onboarding_command', 'onboarding_progress', 'generate_onboarding_rules',
'suggest_follow_ups', 'future_unknown_tool']
tools = [{'type': 'function', 'function': {'name': n, 'parameters': {'type': 'object'}}}
for n in helpers + ['story'] + incompatible]
for native in (False, True):
self.gate.native_agent = native
self.assertEqual(self.request(extra={'tools': tools})[0], 200)
self.assertEqual([t['function']['name'] for t in self.calls[-1]['tools']],
helpers + ([] if native else ['story']))
def test_forced_unavailable_tool_cannot_survive_even_without_a_tool_definition(self):
for extra in ({'tool_choice': {'type': 'function', 'function': {'name': 'clarification'}}},
{'tool_choice': {'type': 'function', 'function': 'malformed'}},
{'tools': [], 'tool_choice': 'required'},
{'tools': [{'type': 'function', 'function': {'name': 'read'}}],
'tool_choice': {'type': 'function', 'function': {'name': 'execute_sql'}}}):
self.assertEqual(self.request(extra=extra)[0], 200)
self.assertNotIn('tool_choice', self.calls[-1])
def test_previous_turn_background_call_cannot_enter_current_thinking_or_provider(self):
marker='quality-turn:current-request'
self.gate._native_turn_marker=marker