OpenHands icon indicating copy to clipboard operation
OpenHands copied to clipboard

Improving `edit_file_by_replace` method of `file_ops` plugin

Open a6kme opened this issue 1 year ago • 2 comments

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_replace commands repeatedly with bad arguments for to_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

  1. We include a new function replace_file_content in file_ops.py plugin which can replace the old file with new content
  2. We modify the prompt of edit_file_by_replace to add something on the lines of If 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!

a6kme avatar Aug 23 '24 12:08 a6kme

Related: https://github.com/All-Hands-AI/OpenHands/issues/3231

mamoodi avatar Aug 23 '24 14:08 mamoodi

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.

github-actions[bot] avatar Sep 23 '24 01:09 github-actions[bot]

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.

github-actions[bot] avatar Oct 24 '24 01:10 github-actions[bot]

This issue was closed because it has been stalled for over 30 days with no activity.

github-actions[bot] avatar Oct 31 '24 02:10 github-actions[bot]