Skip to content

🙌 OpenHands: AI-Driven Development

Bugfix by added config to disable plugin initialization for Persistent sandbox (#2179)

மனோஜ்குமார் பழனிச்சாமி4e479038f99c
diff --git a/opendevin/core/config.py b/opendevin/core/config.py
index e05d0282f5..2bbab5656a 100644
--- a/opendevin/core/config.py
+++ b/opendevin/core/config.py
@@ -179,6 +179,7 @@ class AppConfig(metaclass=Singleton):
disable_color: bool = False
sandbox_user_id: int = os.getuid() if hasattr(os, 'getuid') else 1000
sandbox_timeout: int = 120
+ initialize_plugins: bool = True
persist_sandbox: bool = False
ssh_port: int = 63710
ssh_password: str | None = None
diff --git a/opendevin/core/main.py b/opendevin/core/main.py
index d0937c78f1..9fc3418ca3 100644
--- a/opendevin/core/main.py
+++ b/opendevin/core/main.py
@@ -91,12 +91,7 @@ async def main(
event_stream=event_stream,
)
runtime = ServerRuntime(event_stream=event_stream, sandbox=sandbox)
-
- 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_sandbox_plugins(controller.agent.sandbox_plugins)
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/plugins/mixin.py b/opendevin/runtime/plugins/mixin.py
index 91d229d093..0eadf42def 100644
--- a/opendevin/runtime/plugins/mixin.py
+++ b/opendevin/runtime/plugins/mixin.py
@@ -9,6 +9,9 @@ from opendevin.runtime.plugins.requirement import PluginRequirement
class SandboxProtocol(Protocol):
# https://stackoverflow.com/questions/51930339/how-do-i-correctly-add-type-hints-to-mixin-classes
+ @property
+ def initialize_plugins(self) -> bool: ...
+
def execute(
self, cmd: str, stream: bool = False
) -> tuple[int, str | CancellableStream]: ...
@@ -25,41 +28,48 @@ class PluginMixin:
if hasattr(self, 'plugin_initialized') and self.plugin_initialized:
return
- # clean-up ~/.bashrc and touch ~/.bashrc
- exit_code, output = self.execute('rm -f ~/.bashrc && touch ~/.bashrc')
-
- for requirement in requirements:
- # copy over the files
- self.copy_to(requirement.host_src, requirement.sandbox_dest, recursive=True)
- logger.info(
- f'Copied files from [{requirement.host_src}] to [{requirement.sandbox_dest}] inside sandbox.'
- )
-
- # Execute the bash script
- abs_path_to_bash_script = os.path.join(
- requirement.sandbox_dest, requirement.bash_script_path
- )
- logger.info(
- f'Initializing plugin [{requirement.name}] by executing [{abs_path_to_bash_script}] in the sandbox.'
- )
- exit_code, output = self.execute(abs_path_to_bash_script, stream=True)
- if isinstance(output, CancellableStream):
- for line in output:
- if line.endswith('\n'):
- line = line[:-1]
- _exit_code = output.exit_code()
- output.close()
- if _exit_code != 0:
- raise RuntimeError(
- f'Failed to initialize plugin {requirement.name} with exit code {_exit_code} and output {output}'
- )
- logger.info(f'Plugin {requirement.name} initialized successfully')
- else:
- if exit_code != 0:
- raise RuntimeError(
- f'Failed to initialize plugin {requirement.name} with exit code {exit_code} and output: {output}'
- )
- logger.info(f'Plugin {requirement.name} initialized successfully.')
+ if self.initialize_plugins:
+ logger.info('Initializing plugins in the sandbox')
+
+ # clean-up ~/.bashrc and touch ~/.bashrc
+ exit_code, output = self.execute('rm -f ~/.bashrc && touch ~/.bashrc')
+
+ for requirement in requirements:
+ # copy over the files
+ self.copy_to(
+ requirement.host_src, requirement.sandbox_dest, recursive=True
+ )
+ logger.info(
+ f'Copied files from [{requirement.host_src}] to [{requirement.sandbox_dest}] inside sandbox.'
+ )
+
+ # Execute the bash script
+ abs_path_to_bash_script = os.path.join(
+ requirement.sandbox_dest, requirement.bash_script_path
+ )
+ logger.info(
+ f'Initializing plugin [{requirement.name}] by executing [{abs_path_to_bash_script}] in the sandbox.'
+ )
+ exit_code, output = self.execute(abs_path_to_bash_script, stream=True)
+ if isinstance(output, CancellableStream):
+ for line in output:
+ if line.endswith('\n'):
+ line = line[:-1]
+ _exit_code = output.exit_code()
+ output.close()
+ if _exit_code != 0:
+ raise RuntimeError(
+ f'Failed to initialize plugin {requirement.name} with exit code {_exit_code} and output {output}'
+ )
+ logger.info(f'Plugin {requirement.name} initialized successfully')
+ else:
+ if exit_code != 0:
+ raise RuntimeError(
+ f'Failed to initialize plugin {requirement.name} with exit code {exit_code} and output: {output}'
+ )
+ logger.info(f'Plugin {requirement.name} initialized successfully.')
+ else:
+ logger.info('Skipping plugin initialization in the sandbox')
if len(requirements) > 0:
exit_code, output = self.execute('source ~/.bashrc')
diff --git a/opendevin/runtime/sandbox.py b/opendevin/runtime/sandbox.py
index 72adb3552b..a274a15bf8 100644
--- a/opendevin/runtime/sandbox.py
+++ b/opendevin/runtime/sandbox.py
@@ -20,6 +20,7 @@ class Sandbox(ABC, PluginMixin):
self.add_to_env(sandbox_key, os.environ[key])
if config.enable_auto_lint:
self.add_to_env('ENABLE_AUTO_LINT', 'true')
+ self.initialize_plugins: bool = config.initialize_plugins
def add_to_env(self, key: str, value: str):
self._env[key] = value
diff --git a/opendevin/server/session/agent.py b/opendevin/server/session/agent.py
index 9dd9a26ec1..bf41a88679 100644
--- a/opendevin/server/session/agent.py
+++ b/opendevin/server/session/agent.py
@@ -101,12 +101,7 @@ class AgentSession:
logger.warning(
'CodeActAgent requires DockerSSHBox as sandbox! Using other sandbox that are not stateful (LocalBox, DockerExecBox) will not work properly.'
)
-
- 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_sandbox_plugins(agent.sandbox_plugins)
self.runtime.init_runtime_tools(agent.runtime_tools)
self.controller = AgentController(