144 lines
4.3 KiB
Python
144 lines
4.3 KiB
Python
import pytest
|
|
from src.tool_errors import count_trailing_tool_errors
|
|
|
|
ERROR_STRINGS = (
|
|
"could not find oldstring",
|
|
"found multiple matches",
|
|
"oldstring cannot be empty",
|
|
"no changes to apply",
|
|
)
|
|
|
|
def _assistant_call(call_id: str, name: str) -> dict:
|
|
return {
|
|
"role": "assistant",
|
|
"tool_calls": [
|
|
{
|
|
"id": call_id,
|
|
"function": {"name": name}
|
|
}
|
|
]
|
|
}
|
|
|
|
def _tool_result(call_id: str, content: str | list) -> dict:
|
|
return {
|
|
"role": "tool",
|
|
"tool_call_id": call_id,
|
|
"content": content
|
|
}
|
|
|
|
@pytest.mark.parametrize("err", ERROR_STRINGS)
|
|
def test_single_error_strings(err):
|
|
messages = [
|
|
_assistant_call("c1", "edit"),
|
|
_tool_result("c1", err)
|
|
]
|
|
assert count_trailing_tool_errors(messages) == 1
|
|
|
|
def test_abort_neutral_on_edit():
|
|
messages = [
|
|
_assistant_call("c1", "edit"),
|
|
_tool_result("c1", "tool execution aborted")
|
|
]
|
|
assert count_trailing_tool_errors(messages) == 0
|
|
|
|
def test_bash_non_zero_after_edit_error():
|
|
messages = [
|
|
_assistant_call("c1", "edit"),
|
|
_tool_result("c1", ERROR_STRINGS[0]),
|
|
_assistant_call("c2", "bash"),
|
|
_tool_result("c2", "exit 1")
|
|
]
|
|
# bash is not an edit tool, so it's skipped (no reset).
|
|
assert count_trailing_tool_errors(messages) == 1
|
|
|
|
def test_successful_edit_resets_count():
|
|
messages = [
|
|
_assistant_call("c1", "edit"),
|
|
_tool_result("c1", ERROR_STRINGS[0]),
|
|
_assistant_call("c2", "edit"),
|
|
_tool_result("c2", "Successfully applied patch.")
|
|
]
|
|
assert count_trailing_tool_errors(messages) == 0
|
|
|
|
|
|
def test_three_consecutive_edit_errors():
|
|
messages = [
|
|
_assistant_call("c1", "edit"),
|
|
_tool_result("c1", ERROR_STRINGS[0]),
|
|
_assistant_call("c2", "edit"),
|
|
_tool_result("c2", ERROR_STRINGS[1]),
|
|
_assistant_call("c3", "edit"),
|
|
_tool_result("c3", ERROR_STRINGS[2]),
|
|
]
|
|
assert count_trailing_tool_errors(messages) == 3
|
|
|
|
|
|
def test_err_success_err_trailing_run_is_one():
|
|
messages = [
|
|
_assistant_call("c1", "edit"),
|
|
_tool_result("c1", ERROR_STRINGS[0]),
|
|
_assistant_call("c2", "edit"),
|
|
_tool_result("c2", "Done."),
|
|
_assistant_call("c3", "edit"),
|
|
_tool_result("c3", ERROR_STRINGS[0]),
|
|
]
|
|
assert count_trailing_tool_errors(messages) == 1
|
|
|
|
def test_tool_call_id_with_no_assistant_call_not_counted():
|
|
messages = [
|
|
_tool_result("unknown_id", ERROR_STRINGS[0])
|
|
]
|
|
assert count_trailing_tool_errors(messages) == 0
|
|
|
|
def test_non_dict_junk_no_raise_no_count():
|
|
# tool_call_id with no assistant call -> skipped
|
|
messages = [
|
|
{"role": "tool", "content": ERROR_STRINGS[0]}
|
|
]
|
|
assert count_trailing_tool_errors(messages) == 0
|
|
|
|
|
|
def test_content_list_of_text_parts_matches():
|
|
messages = [
|
|
_assistant_call("c1", "edit"),
|
|
_tool_result("c1", [
|
|
{"type": "text", "text": "Some preamble"},
|
|
{"type": "text", "text": "could not find oldstring in the file."},
|
|
])
|
|
]
|
|
assert count_trailing_tool_errors(messages) == 1
|
|
|
|
|
|
def test_content_json_wrapped_string_matches():
|
|
messages = [
|
|
_assistant_call("c1", "edit"),
|
|
_tool_result("c1", '"could not find oldstring"')
|
|
]
|
|
assert count_trailing_tool_errors(messages) == 1
|
|
|
|
def test_window_cap_60_errors_gives_40():
|
|
messages = []
|
|
for i in range(60):
|
|
cid = f"c{i}"
|
|
messages.append(_assistant_call(cid, "edit"))
|
|
messages.append(_tool_result(cid, ERROR_STRINGS[0]))
|
|
assert count_trailing_tool_errors(messages) == 40
|
|
|
|
def test_empty_messages_list():
|
|
assert count_trailing_tool_errors([]) == 0
|
|
|
|
def test_abort_neutral_middle():
|
|
# [err, err, aborted, err] walking backwards:
|
|
# err (count=1), aborted (skip, count stays 1), err (count=2), err (count=3)
|
|
messages = [
|
|
_assistant_call("c1", "edit"),
|
|
_tool_result("c1", ERROR_STRINGS[0]),
|
|
_assistant_call("c2", "edit"),
|
|
_tool_result("c2", ERROR_STRINGS[1]),
|
|
_assistant_call("c3", "edit"),
|
|
_tool_result("c3", "tool execution aborted"),
|
|
_assistant_call("c4", "edit"),
|
|
_tool_result("c4", ERROR_STRINGS[0]),
|
|
]
|
|
assert count_trailing_tool_errors(messages) == 3
|