Skip to content

Simplify microagent loading #9317

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

Closed
wants to merge 3 commits into from
Closed

Conversation

neubig
Copy link
Contributor

@neubig neubig commented Jun 24, 2025

  • This change is worth documenting at https://docs.all-hands.dev/
  • Include this change in the Release Notes. If checked, you must provide an end-user friendly description for your change below

End-user friendly description of the problem this fixes or functionality this introduces.

This PR simplifies microagent loading by using modern tempfile syntax.

The goal is to fix an error that I was encountering while loading.

^[[92m21:15:34 - openhands:INFO^[[0m: base.py:711 - [runtime 788d4ed644234f90b7cd434920d70a92] Successfully cloned org-level microagents from neubig/.openhands
^[[92m21:15:34 - openhands:DEBUG^[[0m: log_streamer.py:46 - [runtime 788d4ed644234f90b7cd434920d70a92] [inside container] INFO:     192.168.65.1:19812 - "POST /execute_action HTTP/1.1" 200 OK
^[[92m21:15:34 - openhands:DEBUG^[[0m: log_streamer.py:46 - [runtime 788d4ed644234f90b7cd434920d70a92] [inside container] INFO:     192.168.65.1:19812 - "POST /list_files HTTP/1.1" 200 OK
^[[92m21:15:34 - openhands:ERROR^[[0m: base.py:731 - [runtime 788d4ed644234f90b7cd434920d70a92] Error loading org-level microagents: [Errno 2] No such file or directory: '/workspace/org_openhands_neubig'
Traceback (most recent call last):
  File "/Users/gneubig/work/OpenHands/openhands/runtime/base.py", line 723, in get_microagents_from_org_or_user
    shutil.rmtree(org_repo_dir)
  File "/Users/gneubig/miniconda3/lib/python3.12/shutil.py", line 775, in rmtree
    onexc(os.lstat, path, err)
  File "/Users/gneubig/miniconda3/lib/python3.12/shutil.py", line 773, in rmtree
    orig_st = os.lstat(path, dir_fd=dir_fd)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/workspace/org_openhands_neubig'
  File "/Users/gneubig/miniconda3/lib/python3.12/threading.py", line 1030, in _bootstrap
    self._bootstrap_inner()
  File "/Users/gneubig/miniconda3/lib/python3.12/threading.py", line 1073, in _bootstrap_inner
    self.run()
  File "/Users/gneubig/miniconda3/lib/python3.12/threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/gneubig/miniconda3/lib/python3.12/concurrent/futures/thread.py", line 92, in _worker
    work_item.run()
  File "/Users/gneubig/miniconda3/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/Users/gneubig/work/OpenHands/openhands/utils/async_utils.py", line 17, in <lambda>
    coro = loop.run_in_executor(None, lambda: fn(*args, **kwargs))
  File "/Users/gneubig/work/OpenHands/openhands/runtime/base.py", line 753, in get_microagents_from_selected_repo
    org_microagents = self.get_microagents_from_org_or_user(selected_repository)
  File "/Users/gneubig/work/OpenHands/openhands/runtime/base.py", line 731, in get_microagents_from_org_or_user
    self.log('error', f'Error loading org-level microagents: {str(e)}')
  File "/Users/gneubig/work/OpenHands/openhands/runtime/base.py", line 209, in log
    getattr(logger, level)(message, stacklevel=2)
  File "/Users/gneubig/miniconda3/lib/python3.12/logging/__init__.py", line 1568, in error
    self._log(ERROR, msg, args, **kwargs)
  File "/Users/gneubig/miniconda3/lib/python3.12/logging/__init__.py", line 1684, in _log
    self.handle(record)

To run this PR locally, use the following command:

docker run -it --rm   -p 3000:3000   -v /var/run/docker.sock:/var/run/docker.sock   --add-host host.docker.internal:host-gateway   -e SANDBOX_RUNTIME_CONTAINER_IMAGE=docker.all-hands.dev/all-hands-ai/runtime:738840c-nikolaik   --name openhands-app-738840c   docker.all-hands.dev/all-hands-ai/openhands:738840c

@neubig
Copy link
Contributor Author

neubig commented Jun 24, 2025

@OpenHands check the git diff introduced in this PR, and then check if there are other places using rmtree in the same file that could be fixed.

Copy link

openhands-ai bot commented Jun 24, 2025

I'm on it! neubig can track my progress at all-hands.dev

Copy link

openhands-ai bot commented Jun 24, 2025

I've completed the task of checking the git diff in the PR and fixing other places using rmtree in the same file. Here's a summary of what I did:

  1. I examined the PR changes, which introduced a new load_microagents_from_directory method that uses tempfile.TemporaryDirectory as a context manager for safer temporary directory cleanup.

  2. I identified two places in the code where shutil.rmtree() was being used:

    • In the _load_microagents_from_directory method (line 641)
    • In the org repo cloning code (line 774)
  3. I made the following improvements:

    • Updated the _load_microagents_from_directory method to use the new implementation, eliminating the manual cleanup with rmtree
    • Refactored the org repo cloning code to use tempfile.TemporaryDirectory as a context manager, which automatically handles cleanup
    • Removed the unused shutil import since it's no longer needed

These changes make the code safer by ensuring that temporary directories are always cleaned up properly, even if exceptions occur. Using context managers is a more Pythonic approach and follows best practices for resource management.

The changes have been committed and pushed to the branch neubig/simplify_microagent_loading.

@neubig neubig closed this Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants