fix: 允许 Agent 持续自主诊断

This commit is contained in:
cnbugs
2026-07-25 12:50:38 +08:00
parent fdac1eaffc
commit 47abfef238
2 changed files with 74 additions and 4 deletions
+47
View File
@@ -121,6 +121,53 @@ class DiagnosticAgentTest(unittest.TestCase):
self.assertEqual(action["action"], "command")
self.assertEqual(action["command"], "kubectl get pods -A")
def test_ai_mode_continues_beyond_six_commands_until_model_finishes(self):
command_count = 8
responses = iter([
*[
(
'{"action":"command","command":"kubectl get pods -A '
f'--field-selector metadata.name=pod-{index}","reason":"继续收集证据"}}'
)
for index in range(command_count)
],
'{"action":"final","analysis":"已完成根因分析"}',
])
executed = []
async def fake_completion(messages):
return next(responses)
async def fake_execute(command):
executed.append(command)
return {"command": command, "mode": "read", "returncode": 0, "stdout": "evidence", "stderr": ""}
async def run():
with patch("backend.ai_agent._complete_chat", fake_completion), patch("backend.ai_agent.execute_kubectl", fake_execute):
return await collect_events(run_diagnostic_agent("report", execution_mode="ai", max_steps=20))
events = asyncio.run(run())
self.assertEqual(len(executed), command_count + 1)
self.assertEqual(events[-1]["type"], "final")
self.assertEqual(events[-1]["content"], "已完成根因分析")
self.assertNotIn("最大轮次", events[-1]["content"])
def test_safety_limit_reports_agent_error_instead_of_sending_user_to_manual_diagnosis(self):
async def fake_completion(messages):
return '{"action":"command","command":"kubectl get pods -A","reason":"继续调查"}'
async def fake_execute(command):
return {"command": command, "mode": "read", "returncode": 0, "stdout": "same result", "stderr": ""}
async def run():
with patch("backend.ai_agent._complete_chat", fake_completion), patch("backend.ai_agent.execute_kubectl", fake_execute):
return await collect_events(run_diagnostic_agent("report", execution_mode="ai", max_steps=2))
events = asyncio.run(run())
self.assertEqual(events[-1]["type"], "error")
self.assertIn("安全上限", events[-1]["message"])
self.assertNotIn("人工排查", events[-1]["message"])
def test_stops_when_model_returns_invalid_action(self):
async def fake_completion(messages):
return "not-json"