Stop repeated duplicate skill loops

This commit is contained in:
武阳
2026-05-08 10:40:01 +08:00
parent 18cfb60539
commit 9e94648ae6
2 changed files with 163 additions and 0 deletions
+78
View File
@@ -1386,6 +1386,84 @@ class AgentRuntimeTests(unittest.TestCase):
duplicate_payload = json.loads(str(tool_messages[1].get('content', '{}')))
self.assertIn('已在本轮激活', duplicate_payload.get('content', ''))
def test_agent_stops_repeated_duplicate_skill_loop(self) -> None:
skill_call = {
'type': 'function',
'function': {
'name': 'Skill',
'arguments': '{"skill": "verify"}',
},
}
responses = [
{
'choices': [
{
'message': {
'role': 'assistant',
'content': 'I will load the verification skill.',
'tool_calls': [{**skill_call, 'id': 'call_1'}],
},
'finish_reason': 'tool_calls',
}
],
'usage': {'prompt_tokens': 8, 'completion_tokens': 3},
},
{
'choices': [
{
'message': {
'role': 'assistant',
'content': 'I accidentally load it again.',
'tool_calls': [{**skill_call, 'id': 'call_2'}],
},
'finish_reason': 'tool_calls',
}
],
'usage': {'prompt_tokens': 8, 'completion_tokens': 3},
},
{
'choices': [
{
'message': {
'role': 'assistant',
'content': 'I am still stuck loading it again.',
'tool_calls': [{**skill_call, 'id': 'call_3'}],
},
'finish_reason': 'tool_calls',
}
],
'usage': {'prompt_tokens': 8, 'completion_tokens': 3},
},
]
recorded_payloads: list[dict[str, object]] = []
with tempfile.TemporaryDirectory() as tmp_dir:
workspace = Path(tmp_dir)
with patch(
'src.openai_compat.request.urlopen',
side_effect=make_recording_urlopen_side_effect(responses, recorded_payloads),
):
agent = LocalCodingAgent(
model_config=ModelConfig(
model='Qwen/Qwen3-Coder-30B-A3B-Instruct',
base_url='http://127.0.0.1:8000/v1',
),
runtime_config=AgentRuntimeConfig(cwd=workspace, max_turns=50),
)
result = agent.run('Use verify skill')
self.assertEqual(len(recorded_payloads), 3)
self.assertEqual(result.stop_reason, 'duplicate_skill_loop')
self.assertIn('连续重复调用 Skill', result.final_output)
self.assertEqual(result.tool_calls, 3)
tool_messages = [
message for message in result.transcript if message.get('role') == 'tool'
]
self.assertEqual(tool_messages[-1].get('metadata', {}).get('stop_agent'), True)
self.assertEqual(tool_messages[-1].get('metadata', {}).get('duplicate_count'), 2)
self.assertTrue(
any(event.get('type') == 'tool_requested_stop' for event in result.events)
)
def test_agent_records_file_history_for_write_tool(self) -> None:
responses = [
{