OpenHands icon indicating copy to clipboard operation
OpenHands copied to clipboard

Integrate E2B sandbox as an alternative to a Docker container

Open mlejva opened this issue 1 year ago • 14 comments

This is a draft PR for integrating E2B sandbox for OpenDevin's agents. The E2B sandbox is a secure micro VM made for running AI agents and AI generated code in the cloud.

I'll be updating the following todo list as I go:

  • [x] Bring E2B sandbox to the parity in terms of functionality with the current sandbox.py
  • [x] Common interface between Docker container and E2B sandbox
  • [x] Update fileops to happen inside sandbox when using E2B
  • [ ] ~Use E2B sandbox for running swe-bench evals end-to-end~ (will be done in separate PR)

mlejva avatar Apr 04 '24 18:04 mlejva

curious: Can e2b run inside docker?

foragerr avatar Apr 04 '24 18:04 foragerr

curious: Can e2b run inside docker?

No. E2B sandbox is a VM so it sits on a "lower level" compared to a container. What will be possible in the future is to run Docker inside E2B.

mlejva avatar Apr 04 '24 18:04 mlejva

Looking good!

We can probably formalize the sandbox interface with an abstract class--doesn't need to be in this PR though

I'm planning on updating the PR today/tomorrow with proper config and env vars setup and make it match the functionality of the current sandbox. Once that all will look good to you guys, happy to start working on the common interface.

mlejva avatar Apr 05 '24 18:04 mlejva

@rbren a short update, still work in progress.

I updated the PR so the E2B sandbox matches the functionality of the current Docker sandbox. Specifically the background commands are now supported. Also rebased to main to stay up to date with the new code style rules.

Next step is to:

  • [x] 1. Clean up and improve types so we can build a shared interface between E2B sandbox and Docker sandbox
  • [x] 2. Add config option to allow switching between E2B sandbox and Docker sandbox
  • [x] 3. Enable filesystem operations. Currently, the filesystem operations with Docker sandbox are happening through a mounted volume because the container is running locally. WIth E2B sandbox, everything should happen inside the sandbox - sandbox.filesystem.write('...') - you're interacting right with the filesystem inside the sandbox. We can't currently mount a local folder to the sandbox. I'm thinking introducing a config flag (as mentioned above) to allow users switch sandbox and change how filesystem ops work based on that.

I think I should be able to finish the whole PR this Sunday

mlejva avatar Apr 07 '24 05:04 mlejva

@rbren similar to what @foragerr did in #874 I think we should also build a common interface for process (what's known as command at the moment). It'd be something fairly simple like

from abc import ABC, abstractmethod

class Process(ABC):
    @abstractmethod
    def kill(self):
        pass

    @abstractmethod
    def read_logs(self) -> str:
        pass
    
    @property
    @abstractmethod
    def id(self) -> int:
        pass

    @property
    @abstractmethod
    def command(self) -> str:
        pass

    @property
    @abstractmethod
    def exit_code(self) -> int | None: # The reason it can be none is because the process can still be running
       pass 

What do you think?

mlejva avatar Apr 07 '24 21:04 mlejva

@mlejva that seems reasonable to me!

rbren avatar Apr 09 '24 16:04 rbren

curious: Can e2b run inside docker?

@foragerr I realized I might have misunderstood your question. I thought you're talking about self-hosting. With self-hosting what I wrote stands - E2B sandbox is a VM so it sits on a "lower level" compared to a container. What will be possible in the future is to run Docker inside E2B.

But you can definitely use E2B SDK for launching sandboxes in our cloud from Docker and Dockerize the whole app.

I just wanted to clear that up

mlejva avatar Apr 09 '24 19:04 mlejva

Thank you for the review @neubig! I resolved the comments and will fix the build errors today. I'm on the move so I'll get back to my laptop only tonight.

mlejva avatar Apr 14 '24 16:04 mlejva

Hey @rbren thank you so much for the code review. I merged the main branch. I'm struggling a bit with understanding why the build didn't pass though.

I built it locally with both Python 3.11 and 3.12 and it looks like it's passing.

Screenshot 2024-04-15 at 11 24 41 PM Screenshot 2024-04-15 at 11 23 31 PM

The errors in GitHub actions seem to only show error code and nothing else though:

  • https://github.com/OpenDevin/OpenDevin/actions/runs/8675380093/job/23793473005?pr=727
  • https://github.com/OpenDevin/OpenDevin/actions/runs/8675380093/job/23793472708?pr=727
  • https://github.com/OpenDevin/OpenDevin/actions/runs/8675380093/job/23793472924?pr=727
Screenshot 2024-04-15 at 11 25 20 PM

I'm not sure why it's not passing, I you have any tips, it would be super helpful!

mlejva avatar Apr 16 '24 06:04 mlejva

I also did a bit of refactorization in this PR.

  • Sandbox is now separated to docker and e2b
  • BackgroundCommand is now Process with separate `
  • I added readme to E2B sandbox to get people started easily
  • I pre-built a custom E2B sandbox for OpenDevin called open-devin.
    • Currently, it's only 0.5GB RAM and 2vCPU and people need to use their on SDK key but once we get OpenDevin to a cloud hosted webapp, we'll upgrade it to better machine, and sponsor all needed compute for OpenDevin's team API key

mlejva avatar Apr 16 '24 06:04 mlejva

auto-merge enabled! 🚀

Thanks for all the work here!

rbren avatar Apr 17 '24 16:04 rbren

Sorry, merge conflicts 😭

SWE-agent implemented start and end lines for read/write operations, which I suppose we'll need to support with e2b. Hopefully that's not too hard...

rbren avatar Apr 17 '24 16:04 rbren

Sorry, merge conflicts 😭

SWE-agent implemented start and end lines for read/write operations, which I suppose we'll need to support with e2b. Hopefully that's not too hard...

Looking into it in 1-2 hours!

mlejva avatar Apr 17 '24 16:04 mlejva

Hey @rbren I finally got to resolving the conflicts. Sorry it took longer. Can you please check it when you have a moment and if it looks good to you merge it?

Thank you!

mlejva avatar Apr 18 '24 22:04 mlejva