Add regression test for multi tool message fix

This commit is contained in:
Julio César Suástegui 2026-05-22 11:37:26 -06:00
parent cf9cc4b39e
commit d199ed1377
1 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,45 @@
import signal
import pytest
from cai.util import fix_message_list
def test_fix_message_list_handles_multiple_tool_results_without_loop():
if not hasattr(signal, "SIGALRM"):
pytest.skip("SIGALRM is required for this infinite-loop regression test")
messages = [
{"role": "user", "content": "Run both tools"},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_one",
"type": "function",
"function": {"name": "first_tool", "arguments": "{}"},
},
{
"id": "call_two",
"type": "function",
"function": {"name": "second_tool", "arguments": "{}"},
},
],
},
{"role": "tool", "tool_call_id": "call_one", "content": "first result"},
{"role": "tool", "tool_call_id": "call_two", "content": "second result"},
]
def fail_on_timeout(_signum, _frame):
raise TimeoutError("fix_message_list did not terminate")
previous_handler = signal.signal(signal.SIGALRM, fail_on_timeout)
signal.alarm(2)
try:
fixed_messages = fix_message_list(messages)
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, previous_handler)
assert fixed_messages == messages