Skip to content

🙌 OpenHands: AI-Driven Development

Initialized plugins only once for persistent sandboxes (#2162)

மனோஜ்குமார் பழனிச்சாமி3a4dc5c68cf9
diff --git a/opendevin/core/main.py b/opendevin/core/main.py
index 9fc3418ca3..d0937c78f1 100644
--- a/opendevin/core/main.py
+++ b/opendevin/core/main.py
@@ -91,7 +91,12 @@ async def main(
event_stream=event_stream,
)
runtime = ServerRuntime(event_stream=event_stream, sandbox=sandbox)
- runtime.init_sandbox_plugins(controller.agent.sandbox_plugins)
+
+ if runtime.sandbox.is_initial_session:
+ logger.info('Initializing plugins in the sandbox')
+ runtime.init_sandbox_plugins(controller.agent.sandbox_plugins)
+ else:
+ logger.info('Plugins are already initialized in the sandbox')
runtime.init_runtime_tools(controller.agent.runtime_tools, is_async=False)
await event_stream.add_event(MessageAction(content=task), EventSource.USER)
diff --git a/opendevin/runtime/docker/ssh_box.py b/opendevin/runtime/docker/ssh_box.py
index 40218450f3..7d1503ee19 100644
--- a/opendevin/runtime/docker/ssh_box.py
+++ b/opendevin/runtime/docker/ssh_box.py
@@ -238,11 +238,11 @@ class DockerSSHBox(Sandbox):
self._ssh_port = find_available_tcp_port()
try:
docker.DockerClient().containers.get(self.container_name)
- is_initial_session = False
+ self.is_initial_session = False
except docker.errors.NotFound:
- is_initial_session = True
+ self.is_initial_session = True
logger.info('Creating new Docker container')
- if not config.persist_sandbox or is_initial_session:
+ if not config.persist_sandbox or self.is_initial_session:
n_tries = 5
while n_tries > 0:
try:
diff --git a/opendevin/runtime/sandbox.py b/opendevin/runtime/sandbox.py
index 64631beb67..72adb3552b 100644
--- a/opendevin/runtime/sandbox.py
+++ b/opendevin/runtime/sandbox.py
@@ -11,6 +11,7 @@ from opendevin.runtime.plugins.mixin import PluginMixin
class Sandbox(ABC, PluginMixin):
background_commands: dict[int, Process] = {}
_env: dict[str, str] = {}
+ is_initial_session: bool = True
def __init__(self, **kwargs):
for key in os.environ:
diff --git a/opendevin/server/session/agent.py b/opendevin/server/session/agent.py
index bf41a88679..9dd9a26ec1 100644
--- a/opendevin/server/session/agent.py
+++ b/opendevin/server/session/agent.py
@@ -101,7 +101,12 @@ class AgentSession:
logger.warning(
'CodeActAgent requires DockerSSHBox as sandbox! Using other sandbox that are not stateful (LocalBox, DockerExecBox) will not work properly.'
)
- self.runtime.init_sandbox_plugins(agent.sandbox_plugins)
+
+ if self.runtime.sandbox.is_initial_session:
+ logger.info('Initializing plugins in the sandbox')
+ self.runtime.init_sandbox_plugins(agent.sandbox_plugins)
+ else:
+ logger.info('Plugins are already initialized in the sandbox')
self.runtime.init_runtime_tools(agent.runtime_tools)
self.controller = AgentController(