From ce24a47cdfe203183d908d5c00d63324655cdb19 Mon Sep 17 00:00:00 2001 From: Johannes du Plessis Date: Wed, 3 Jun 2026 16:47:08 -0700 Subject: [PATCH] fix: auto-create local sandbox root dir (#1402) create_local_sandbox now mkdir -p's the resolved root dir, so a custom LOCAL_SANDBOX_ROOT_DIR (or a /tmp path cleared on reboot) no longer fails sandbox work-dir resolution. --- agent/integrations/local.py | 4 +++- tests/test_local_integration.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/test_local_integration.py diff --git a/agent/integrations/local.py b/agent/integrations/local.py index dd1bd9d6..7cff4509 100644 --- a/agent/integrations/local.py +++ b/agent/integrations/local.py @@ -10,7 +10,8 @@ def create_local_sandbox(sandbox_id: str | None = None): Only use for local development with human-in-the-loop enabled. The root directory defaults to the current working directory and can be - overridden via the LOCAL_SANDBOX_ROOT_DIR environment variable. + overridden via the LOCAL_SANDBOX_ROOT_DIR environment variable. It is + created if it does not already exist. Args: sandbox_id: Ignored for local sandboxes; accepted for interface compatibility. @@ -19,6 +20,7 @@ def create_local_sandbox(sandbox_id: str | None = None): LocalShellBackend instance implementing SandboxBackendProtocol. """ root_dir = os.getenv("LOCAL_SANDBOX_ROOT_DIR", os.getcwd()) + os.makedirs(root_dir, exist_ok=True) return LocalShellBackend( root_dir=root_dir, diff --git a/tests/test_local_integration.py b/tests/test_local_integration.py new file mode 100644 index 00000000..b1ebba48 --- /dev/null +++ b/tests/test_local_integration.py @@ -0,0 +1,29 @@ +import agent.integrations.local as local_mod + + +class _StubLocalShellBackend: + def __init__(self, *, root_dir, inherit_env): + self.root_dir = root_dir + self.inherit_env = inherit_env + + +def test_create_local_sandbox_creates_missing_root_dir(monkeypatch, tmp_path): + root = tmp_path / "nested" / "openswe-sandbox" + monkeypatch.setenv("LOCAL_SANDBOX_ROOT_DIR", str(root)) + monkeypatch.setattr(local_mod, "LocalShellBackend", _StubLocalShellBackend) + + backend = local_mod.create_local_sandbox() + + assert root.is_dir() + assert backend.root_dir == str(root) + assert backend.inherit_env is True + + +def test_create_local_sandbox_defaults_to_cwd(monkeypatch, tmp_path): + monkeypatch.delenv("LOCAL_SANDBOX_ROOT_DIR", raising=False) + monkeypatch.chdir(tmp_path) + monkeypatch.setattr(local_mod, "LocalShellBackend", _StubLocalShellBackend) + + backend = local_mod.create_local_sandbox() + + assert backend.root_dir == str(tmp_path)