Improving `edit_file_by_replace` method of `file_ops` plugin
Summary
I was playing with OpenHands and noticed that agent makes suboptimal decisions when using edit_file_by_replace. Here is an instance where it goes into an suboptimal route trying to fix a test file. The issue can be summarised as below.
- There was a python file in the workspace
1|import pytest 2|from flask import Flask 3|from focal_code import app 4| 5|@pytest.fixture 6|def client(): 7| with app.test_client() as client: 8| yield client 9| 10|def test_full_ssrf(client, requests_mock): 11| # Mock the external request 12| requests_mock.get("https://test.example.com/data/", text="mocked response") 13| 14| # Simulate a request to the endpoint 15| response = client.get("/full_ssrf?subdomain=test") 16| 17| # Check the response 18| assert response.status_code == 200 19| assert response.data.decode() == "mocked response" - The agent decided to add an import
import requests_mock - It ended up issuing
edit_file_by_replacecommands repeatedly with bad arguments forto_replace.
EDITED_CODE = """import pytest
import requests_mock
from flask import Flask
from focal_code import app
@pytest.fixture
def client():
with app.test_client() as client:
yield client
def test_full_ssrf(client, requests_mock):
# Mock the external request
requests_mock.get("https://test.example.com/data/", text="mocked response")
# Simulate a request to the endpoint
response = client.get("/full_ssrf?subdomain=test")
# Check the response
assert response.status_code == 200
assert response.data.decode() == "mocked response"
"""
edit_file_by_replace('test.py', to_replace="from flask import Flask", new_content=EDITED_CODE)
Motivation We would like to avoid similar actions by the agent.
Technical Design There are two choices that I can think of to improve the plugin
- We include a new function
replace_file_contentinfile_ops.pyplugin which can replace the old file with new content - We modify the prompt of
edit_file_by_replaceto add something on the lines ofIf you wish to replace the content of the entire file, set to_replace to the entire content of the file.
Alternatives to Consider
Additional context I was using OpenAI's GPT-4 model for session.
Thank you!
Related: https://github.com/All-Hands-AI/OpenHands/issues/3231
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.
This issue was closed because it has been stalled for over 30 days with no activity.