Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP, Agent] Extension editing experiments: removing edit_file_by_line #2846

Closed
wants to merge 54 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
d3a2d10
add replace-based block edit & preliminary test case fix
xingyaoww Jun 28, 2024
b03fd2c
further fix the insert behavior
xingyaoww Jun 28, 2024
3700478
make edit only work on first occurence
xingyaoww Jun 28, 2024
8138371
bump codeact version since we now use new edit agentskills
xingyaoww Jun 28, 2024
45283e1
update prompt for new agentskills
xingyaoww Jun 28, 2024
790b94f
update integration tests
xingyaoww Jun 28, 2024
8e630c5
make run_infer.sh executable
xingyaoww Jun 28, 2024
693a53f
remove code block for edit_file
xingyaoww Jun 28, 2024
7fcba43
update integration test for prompt changes
xingyaoww Jun 28, 2024
22de134
default to not use hint for eval
xingyaoww Jun 29, 2024
4ec06f0
fix insert emptyfile bug
xingyaoww Jun 29, 2024
2c85f43
throw value error when `to_replace` is empty
xingyaoww Jun 29, 2024
621a158
make `_edit_or_insert_file` return string so we can try to fix some l…
xingyaoww Jun 29, 2024
4cb8bdb
add todo
xingyaoww Jun 29, 2024
20cdd32
update integration test
xingyaoww Jun 29, 2024
db14924
fix sandbox test for this PR
xingyaoww Jul 1, 2024
00f57e9
fix inserting with additional newline
xingyaoww Jul 1, 2024
a2f0916
rename to edit_file_by_replace
xingyaoww Jul 1, 2024
cbf2298
add back `edit_file_by_line`
xingyaoww Jul 1, 2024
fb0b9de
update prompt for new editing tool
xingyaoww Jul 1, 2024
cf9eef9
fix integration tests
xingyaoww Jul 1, 2024
a251122
bump codeact version since there are more changes
xingyaoww Jul 1, 2024
0aa476c
add back append file
xingyaoww Jul 1, 2024
ab47585
fix current line for append
xingyaoww Jul 1, 2024
eba37d5
fix append unit tests
xingyaoww Jul 1, 2024
22274be
change the location where we show edited line no to agent and fix tests
xingyaoww Jul 1, 2024
a45953d
update integration tests
xingyaoww Jul 2, 2024
f85c847
fix global window size affect by open_file bug
xingyaoww Jul 2, 2024
62480e5
fix global window size affect by open_file bug
xingyaoww Jul 2, 2024
8968e80
increase window size to 300
xingyaoww Jul 2, 2024
84b3358
add file beginning and ending marker to avoid looping
xingyaoww Jul 2, 2024
53ef016
Merge commit '41ddba84bd0d62f8a4bc48d08addde4b4269a687' into HEAD
xingyaoww Jul 2, 2024
26d9c90
expand the editor window to better display edit error for model
xingyaoww Jul 2, 2024
a4e41ed
refractor to breakdown edit to internal functions
xingyaoww Jul 2, 2024
eec2aa4
reduce window to 200
xingyaoww Jul 2, 2024
31ae269
move window to 100
xingyaoww Jul 2, 2024
53c36e1
refractor to cleanup some logic into _calculate_window_bounds
xingyaoww Jul 2, 2024
ef80308
Merge branch 'main' into xw/further-improve-edit
neubig Jul 3, 2024
e426298
fix integration tests
xingyaoww Jul 4, 2024
1d6a39e
fix sandbox test on new prompt
xingyaoww Jul 4, 2024
6bba835
update demonstration with new changes
xingyaoww Jul 4, 2024
738c657
fix integration
xingyaoww Jul 4, 2024
8fe5034
initialize llm inside process_instance to circumvent "AttributeError:…
xingyaoww Jul 5, 2024
d2a49aa
update kwargs
xingyaoww Jul 5, 2024
64938f5
retry for internal server error
xingyaoww Jul 5, 2024
21246fb
fix max iteration
xingyaoww Jul 5, 2024
f9cfecd
override max iter from config
xingyaoww Jul 5, 2024
4c6fa51
Merge commit 'adf1a0d55654d0065b615d0b4a6534cc5478b732' into xw/furth…
xingyaoww Jul 7, 2024
5247bfa
fix integration tests
xingyaoww Jul 7, 2024
949b38d
remove edit file by line
xingyaoww Jul 7, 2024
b4ac713
fix integration tests
xingyaoww Jul 7, 2024
06fd2c5
add instruction to avoid hanging
xingyaoww Jul 8, 2024
e690830
Revert "add instruction to avoid hanging"
xingyaoww Jul 9, 2024
57823b3
handle content policy violation error
xingyaoww Jul 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
expand the editor window to better display edit error for model
  • Loading branch information
xingyaoww committed Jul 2, 2024
commit 26d9c90288d7065f98c7c164171205423a4d4c2f
52 changes: 35 additions & 17 deletions opendevin/runtime/plugins/agent_skills/agentskills.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ def _edit_file_impl(
src_abs_path = os.path.abspath(file_name)
first_error_line = None
try:
n_added_lines = None
# Create a temporary file
with tempfile.NamedTemporaryFile('w', delete=False) as temp_file:
temp_file_path = temp_file.name
Expand All @@ -401,35 +402,34 @@ def _edit_file_impl(
lines = original_file.readlines()

if is_append:
content_lines = content.splitlines(keepends=True)
n_added_lines = len(content_lines)
if lines and not (len(lines) == 1 and lines[0].strip() == ''):
# file is not empty
if not lines[-1].endswith('\n'):
lines[-1] += '\n'
content_lines = content.splitlines(keepends=True)
new_lines = lines + content_lines
content = ''.join(new_lines)
else:
# file is empty
content = ''.join(content_lines)

elif is_insert:
inserted_lines = [
content + '\n' if not content.endswith('\n') else content
]
if len(lines) == 0:
new_lines = [
content + '\n' if not content.endswith('\n') else content
]
new_lines = inserted_lines
elif start is not None:
if len(lines) == 1 and lines[0].strip() == '':
# if the file with only 1 line and that line is empty
lines = []

if len(lines) == 0:
new_lines = [
content + '\n' if not content.endswith('\n') else content
]
new_lines = inserted_lines
else:
new_lines = (
lines[: start - 1]
+ [
content + '\n'
if not content.endswith('\n')
else content
]
+ lines[start - 1 :]
lines[: start - 1] + inserted_lines + lines[start - 1 :]
)
else:
assert start is None
Expand All @@ -438,8 +438,10 @@ def _edit_file_impl(
f'Invalid line number: {start}. Line numbers must be between 1 and {len(lines)} (inclusive).\n'
f'{ERROR_MSG_SUFFIX}'
) + '\n'
return ret_str

content = ''.join(new_lines)
n_added_lines = len(inserted_lines)
else:
# Handle cases where start or end are None
if start is None:
Expand Down Expand Up @@ -468,6 +470,7 @@ def _edit_file_impl(
if not content.endswith('\n'):
content += '\n'
content_lines = content.splitlines(True)
n_added_lines = len(content_lines)
new_lines = lines[: start - 1] + content_lines + lines[end:]
content = ''.join(new_lines)

Expand All @@ -493,22 +496,37 @@ def _edit_file_impl(
lint_error, first_error_line = _lint_file(file_name)
if lint_error is not None:
if first_error_line is not None:
CURRENT_LINE = int(first_error_line)
show_line = int(first_error_line)
elif is_append:
# original end-of-file
show_line = len(lines)
# insert OR edit WILL provide meaningful line numbers
elif start is not None and end is not None:
show_line = int((start + end) / 2)
else:
raise ValueError('Invalid state. This should never happen.')

ret_str += LINTER_ERROR_MSG
ret_str += lint_error + '\n'

editor_lines = n_added_lines + 20

ret_str += '[This is how your edit would have looked if applied]\n'
ret_str += '-------------------------------------------------\n'
ret_str += (
_print_window(file_name, CURRENT_LINE, 10, return_str=True) + '\n'
_print_window(file_name, show_line, editor_lines, return_str=True)
+ '\n'
)
ret_str += '-------------------------------------------------\n\n'

ret_str += '[This is the original code before your edit]\n'
ret_str += '-------------------------------------------------\n'
ret_str += (
_print_window(
original_file_backup_path, CURRENT_LINE, 10, return_str=True
original_file_backup_path,
show_line,
editor_lines,
return_str=True,
)
+ '\n'
)
Expand Down
42 changes: 31 additions & 11 deletions tests/unit/test_agent_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,10 @@ def check(any_int):
+ 'E999 IndentationError: unexpected indent\n'
'[This is how your edit would have looked if applied]\n'
'-------------------------------------------------\n'
'(3 more lines above)\n'
'(this is the beginning of the file)\n'
'1|def any_int(a, b, c):\n'
'2| return isinstance(a, int) and isinstance(b, int) and isinstance(c, int)\n'
'3|\n'
'4|def test_any_int():\n'
'5| assert any_int(1, 2, 3) == True\n'
'6| assert any_int(1.5, 2, 3) == False\n'
Expand All @@ -469,12 +472,22 @@ def check(any_int):
'12| assert any_int(0, 0, 0) == True\n'
'13| assert any_int(-1, -2, -3) == True\n'
'14| assert any_int(1, -2, 3) == True\n'
'(19 more lines below)\n'
'15| assert any_int(1.5, -2, 3) == False\n'
'16| assert any_int(1, -2.5, 3) == False\n'
'17|\n'
'18|def check(any_int):\n'
'19| # Check some simple cases\n'
'20| assert any_int(2, 3, 1)==True, "This prints if this assert fails 1 (good for debugging!)"\n'
'21| assert any_int(2.5, 2, 3)==False, "This prints if this assert fails 2 (good for debugging!)"\n'
'(12 more lines below)\n'
'-------------------------------------------------\n'
'\n'
'[This is the original code before your edit]\n'
'-------------------------------------------------\n'
'(3 more lines above)\n'
'(this is the beginning of the file)\n'
'1|def any_int(a, b, c):\n'
'2| return isinstance(a, int) and isinstance(b, int) and isinstance(c, int)\n'
'3|\n'
'4|def test_any_int():\n'
'5| assert any_int(1, 2, 3) == True\n'
'6| assert any_int(1.5, 2, 3) == False\n'
Expand All @@ -486,7 +499,14 @@ def check(any_int):
'12| assert any_int(0, 0, 0) == True\n'
'13| assert any_int(-1, -2, -3) == True\n'
'14| assert any_int(1, -2, 3) == True\n'
'(19 more lines below)\n'
'15| assert any_int(1.5, -2, 3) == False\n'
'16| assert any_int(1, -2.5, 3) == False\n'
'17|\n'
'18|def check(any_int):\n'
'19| # Check some simple cases\n'
'20| assert any_int(2, 3, 1)==True, "This prints if this assert fails 1 (good for debugging!)"\n'
'21| assert any_int(2.5, 2, 3)==False, "This prints if this assert fails 2 (good for debugging!)"\n'
'(12 more lines below)\n'
'-------------------------------------------------\n'
'Your changes have NOT been applied. Please fix your edit command and try again.\n'
'You either need to 1) Specify the correct start/end line arguments or 2) Correct your edit code.\n'
Expand Down Expand Up @@ -1408,17 +1428,17 @@ def test_lint_file_fail_undefined_name_long(tmp_path, monkeypatch, capsys):
f'ERRORS:\n{error_message}\n'
'[This is how your edit would have looked if applied]\n'
'-------------------------------------------------\n'
'(494 more lines above)\n'
+ _numbered_test_lines(error_line - 5, error_line - 1)
'(489 more lines above)\n'
+ _numbered_test_lines(error_line - 10, error_line - 1)
+ '500|undefined_name()\n'
+ _numbered_test_lines(error_line + 1, error_line + 5)
+ '(496 more lines below)\n'
+ _numbered_test_lines(error_line + 1, error_line + 10)
+ '(491 more lines below)\n'
+ '-------------------------------------------------\n\n'
'[This is the original code before your edit]\n'
'-------------------------------------------------\n'
'(494 more lines above)\n'
+ _numbered_test_lines(error_line - 5, error_line + 5)
+ '(495 more lines below)\n'
'(489 more lines above)\n'
+ _numbered_test_lines(error_line - 10, error_line + 10)
+ '(490 more lines below)\n'
+ '-------------------------------------------------\n'
'Your changes have NOT been applied. Please fix your edit command and try again.\n'
'You either need to 1) Specify the correct start/end line arguments or 2) Correct your edit code.\n'
Expand Down