OpenHands icon indicating copy to clipboard operation
OpenHands copied to clipboard

[Bug]: CodeAgent issues with venv management

Open Pychnight opened this issue 9 months ago • 5 comments

Is there an existing issue for the same bug?

  • [X] I have checked the troubleshooting document at https://opendevin.github.io/OpenDevin/modules/usage/troubleshooting
  • [X] I have checked the existing issues.

Describe the bug

Right now, it seems OpenDevin is really really bad at creating and managing virtual envs or testing them, I have tried claude, gpt4, gpt3.5. they all seem to get stuck when trying to run files.

python3 -m venv my_env source my_env/bin/activate

There are also some other situations that that code agent completely forgets it has access to file creation, modification and other elements of being a agent. (this is already been reported in anther issue) and sometimes on chat gpt 3.5 it will get stuck in a endless loop of saying the same thing over and over without actually doing the task.

Current Version

opendevin:main

Installation and Configuration

create a test case around creating, modify and examining and running files within a venv within opendevin

Model and Agent

CodeAgent

Reproduction Steps

1- tell it to manage a file within a venv and tell it to create 4-5 files within that venv and tell it to resolve a error with a module 'sys' has no attribute 'warnoptions' error issue. 2- it will proceed and usually get to the point where it actually has to enter the venv and usually gets stuck around that and tends to get stuck trying to run the python file and never gets the next response / corrects it.

Logs, Errors, Screenshots, and Additional Context

No response

Pychnight avatar May 12 '24 15:05 Pychnight

enter the venv and usually gets stuck around that

What do you mean by gets stuck? Does the command line actually get stuck (hangs) - or the model just stuck in some sort of loops that did not get the task solved?

If former, i suspect it might has something to do with how pxssh works: when a venv is activated, there's a pre-fix added (e.g., (venv) $) which messes up pxssh's parsing. -- @iFurySt is working on an alternative paramiko to pxssh, which hopefully might fix this.

If latter, it will be great if you could provide some screenshots / trajectories of model / agent behavior to help us debugging issues.

xingyaoww avatar May 12 '24 20:05 xingyaoww

visually looks it looks stuck / hanging / frozen. Screenshot 2024-05-12 201807

it gets stuck on step 6 usually during the entry to a python file within a venv, it won't do anything else after step 6... the only way for it to get unstuck is restarting the agent to a new task, attempting to resume the same task will not have it progress any further then this specific step.

There is no error logs ether and can't really tell what the agent is thinking ether as it's a blank state.

seems likely the agent is misunderstanding the context of the message or the error confusing the entire sys to be missing rather then what the actual error.. it's last thoughts where before on step 5 was this

"To ensure the program works correctly, let's try running it:
COMMAND:
python main.py my_env
15:30:18 - OBSERVATION
CmdOutputObservation(content='Traceback (most recent call last):\r\n  File "/workspace/agentsystembot/main.py", line 155, in <module>\r\n    main()\r\n  File "/workspace/agentsystembot/main.py", line 113, in main\r\n    install_requirements(conda_env_name)\r\n  File "/workspace/agentsystembot/main.py", line 23, in install_requirements\r\n    subprocess.check_call(["conda", "create", "--name", conda_env_name, "python=3.9", "--yes"])\r\n  File "/usr/lib/python3.10/subprocess.py", line 364, in check_call\r\n    retcode = call(*popenargs, **kwargs)\r\n  File "/usr/lib/python3.10/subprocess.py", line 345, in call\r\n    with Popen(*popenargs, **kwargs) as p:\r\n  File "/usr/lib/python3.10/subprocess.py", line 971, in __init__\r\n    self._execute_child(args, executable, preexec_fn, close_fds,\r\n  File "/usr/lib/python3.10/subprocess.py", line 1793, in _execute_child\r\n    for dir in os.get_exec_path(env))\r\n  File "/usr/lib/python3.10/os.py", line 630, in get_exec_path\r\n    import warnings\r\n  File "/usr/lib/python3.10/warnings.py", line 538, in <module>\r\n    _processoptions(sys.warnoptions)\r\nAttributeError: module \'sys\' has no attribute \'warnoptions\'', command_id=-1, command='python main.py my_env', exit_code=1, observation='run')"

which a user can see that it was trying to run a python file and solve the attribute error... but it thought "sys" module was missing rather then the more important " no attribute 'warnoptions'', " section of the error prompt. which after it executed this it went to step 6 which is where it froze.

EDIT: seems like your commit did sort of resolve this issue, though it seems to still failing on editing files within the venv her questions or if you'd like me to review any other files in the agent project.", wait_for_response=True, action='message') 05:56:42 - opendevin:INFO: agent_controller.py:201 - Setting agent(CodeActAgent) state from AgentState.RUNNING to AgentState.AWAITING_USER_INPUT 05:56:42 - opendevin:INFO: session.py:40 - WebSocket disconnected, sid: 40979f94-1a8e-45f1-955b-4535cb6cbf45 INFO: connection closed

Pychnight avatar May 13 '24 03:05 Pychnight

can't really tell what the agent is thinking ether as it's a blank state.

ssh into the sandbox (port and pwd provided in the logs) or check python opendevin/runtime/docker/ssh_box.py to interact with sandbox.

SmartManoj avatar May 13 '24 06:05 SmartManoj

Same for me - it simply stocks at certain steps without telling me anything. Its not clear what happens there: From Logs

STEP 7

16:36:11 - opendevin:INFO: prompt.py:158
INFO
HINT:
You should think about the command you just ran, what output it gave, and how that affects your plan.
16:36:13 - ACTION
**CmdRunAction**
COMMAND:
source evm_copy_trading_app/venv/bin/activate && pip install Flask

stanistolberg avatar May 15 '24 16:05 stanistolberg

I think this is an issue caused by pxssh -- I think pxssh works by trying to look for a command line prompt (and it is assumed to not change, e.g., username@hostname $). But when you activate an environment, it will typically change the prompt into something like (env_name) username@hostname $ which will break the existing parsing logic... We will try to fix it very soon :)

xingyaoww avatar May 15 '24 23:05 xingyaoww

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 Jun 15 '24 01:06 github-actions[bot]

In addition to what @xingyaoww mentioned above, the issue in the original post could be a Python mismatch in general.

I haven't had the need to dig around different environments, so I asked my buddy Sonnet 😏 by providing the log's error message and the scenario it is happening in. It is not a "definitive" solution, but as food for thought might help to prompt differently to avoid this pitfall?

Thank you for the clarification. This additional context helps to better understand the situation.
Let's analyze the scenario:

1. The end-user is using a tool that runs Python 3.10 or 3.11.
2. The AI is attempting to create a virtual environment (venv) as part of its task.
3. The error occurs during this process of creating or setting up the venv.

Given this information, here's a more targeted analysis and some potential solutions:

1. Venv Creation Process:
   The error is likely occurring during the venv creation or activation process. The `sys.warnoptions` error suggests that the Python interpreter being used for this process is having issues.

2. Potential Conflict:
   There might be a conflict between the system Python (3.10 or 3.11) and the Python version being used to create the venv.

3. Environment Variables:
   The error could be caused by conflicting environment variables or PATH settings.

Suggested solutions:

1. Specify Python Version Explicitly:
   When creating the venv, explicitly specify the Python version:

   ```python
   import sys
   import subprocess

   python_executable = sys.executable
   subprocess.run([python_executable, "-m", "venv", "my_env_name"])

This ensures that the same Python version running the script is used to create the venv.

  1. Use venv Module Directly: Instead of using subprocess, try using the venv module directly:

    import venv
    venv.create("my_env_name", with_pip=True)
    
  2. Error Handling and Logging: Implement more robust error handling and logging to capture more details about the environment:

    import sys
    import subprocess
    import logging
    
    logging.basicConfig(level=logging.DEBUG)
    
    try:
        logging.debug(f"Python version: {sys.version}")
        logging.debug(f"Python executable: {sys.executable}")
        result = subprocess.run([sys.executable, "-m", "venv", "my_env_name"], 
                                capture_output=True, text=True, check=True)
        logging.debug(f"Venv creation output: {result.stdout}")
    except subprocess.CalledProcessError as e:
        logging.error(f"Failed to create venv: {e}")
        logging.error(f"Error output: {e.output}")
    except Exception as e:
        logging.exception("Unexpected error occurred")
    
  3. Check for Conda Environment: If Conda is being used, ensure it's not interfering with the venv creation:

    import os
    import sys
    
    if 'CONDA_PREFIX' in os.environ:
        print("Warning: Running in a Conda environment. This might interfere with venv creation.")
    
  4. Use virtualenv as an Alternative: If venv continues to cause issues, consider using virtualenv as an alternative:

    import subprocess
    
    subprocess.run(["pip", "install", "virtualenv"])
    subprocess.run(["virtualenv", "my_env_name"])
    

These approaches should help isolate and potentially resolve the issue. The key is to ensure that the correct Python interpreter is being used and that there are no conflicts with the existing environment. Additionally, more detailed logging will help in diagnosing the exact point of failure if the issue persists.

tobitege avatar Jun 26 '24 12:06 tobitege

This issue is probably the same, right? https://github.com/OpenDevin/OpenDevin/issues/2799

mamoodi avatar Jul 06 '24 16:07 mamoodi

@Pychnight @stanistolberg Could you ask the agent to not use Venv as it is not needed because it is running in an isolated sandbox docker container?

SmartManoj avatar Jul 07 '24 03:07 SmartManoj

Going to close this as a duplicate of https://github.com/OpenDevin/OpenDevin/issues/2799 since it seems to be stuck on activating the virtual environment and the other issue has comments on finding the issue.

mamoodi avatar Jul 16 '24 15:07 mamoodi