Python
OpenShell 0.1.2: setup writes the gateway settings that folder sharing needs
Rohit C Prasadc6b2ecc87cd6
diff --git a/coworker/sandbox/setup_cmd.py b/coworker/sandbox/setup_cmd.pyindex 7c220c95..d094597f 100644--- a/coworker/sandbox/setup_cmd.py+++ b/coworker/sandbox/setup_cmd.py@@ -36,6 +36,11 @@ from typing import Any, Callable, Optionalfrom .. import config as app_configfrom .providers import openshell, openshell_policyfrom .providers.openshell import PINNED_VERSION++try:+ import tomllib # stdlib since 3.11+except ModuleNotFoundError: # pragma: no cover - Python 3.10+ import tomli as tomllib # type: ignore[no-redef]from .selection import openshell_problem, select_INSTALLER = "https://raw.githubusercontent.com/NVIDIA/OpenShell/main/install.sh"@@ -46,7 +51,15 @@ DOCKER_DESKTOP_URL = "https://docs.docker.com/desktop/setup/install/mac-install/# What a Mac user is told when Docker Desktop's Linux kernel lacks Landlock (engine 28.0.4,# kernel 6.10.14-linuxkit, seen 2026-09-28; engine 29.8.1, kernel 7.0.14, has it).DOCKER_LANDLOCK_FIX = "Update Docker Desktop: this version's Linux kernel has no Landlock, which OpenShell needs"-_BIND_MOUNTS = "[openshell.drivers.docker]\nenable_bind_mounts = true\n"+# The gateway settings that let a sandbox be given the session's folders (OpenShell 0.1:+# config schema 2; driver config and bind mounts are both off until switched on, and a+# bind mount is refused while resource admission is on). All three, or `sandbox create`+# with our folder mounts is refused.+_BIND_MOUNTS = (+ "[openshell]\nversion = 2\n\n"+ "[openshell.drivers.docker]\nallow_driver_config = true\nenable_bind_mounts = true\n\n"+ "[openshell.drivers.docker.resource_admission]\nenabled = false\n"+)# The base image is pulled by the first `sandbox create` otherwise, which on a slow link# outlives the create's timeout and hangs the first session (OPE-205). So it is a check of# its own, and `setup` offers the download with Docker's own progress and no time limit.@@ -271,10 +284,12 @@ def apply_bind_mounts() -> Optional[str]:gateway_toml, gateway_env = home / "gateway.toml", home / "gateway.env"home.mkdir(parents=True, exist_ok=True)existing = gateway_toml.read_text(encoding="utf-8") if gateway_toml.is_file() else ""- if "[openshell.drivers.docker]" in existing and not _allows_bind_mounts(gateway_toml):- return f"{gateway_toml} already has a [openshell.drivers.docker] table; add `enable_bind_mounts = true` to it by hand."+ if existing.strip() and not _allows_bind_mounts(gateway_toml):+ # Someone's own settings: merging tables into them by text could break the file,+ # and a gateway that cannot read its config does not start.+ return f"{gateway_toml} already has settings. Add these to it by hand, then run setup again:\n" + _BIND_MOUNTSif not _allows_bind_mounts(gateway_toml):- gateway_toml.write_text((existing.rstrip() + "\n\n" if existing.strip() else "") + _BIND_MOUNTS, encoding="utf-8")+ gateway_toml.write_text(_BIND_MOUNTS, encoding="utf-8")env_line = f"OPENSHELL_GATEWAY_CONFIG={gateway_toml}\n"env_text = gateway_env.read_text(encoding="utf-8") if gateway_env.is_file() else ""if "OPENSHELL_GATEWAY_CONFIG=" not in env_text:@@ -299,10 +314,17 @@ def restart_gateway() -> Optional[str]:def _allows_bind_mounts(config: Optional[Path]) -> bool:+ """Whether this gateway config lets a sandbox be given folders: the docker driver takes+ driver config and bind mounts, and resource admission is off for it."""try:- return config is not None and config.is_file() and "enable_bind_mounts = true" in config.read_text(encoding="utf-8")- except OSError:+ if config is None or not config.is_file():+ return False+ with open(config, "rb") as fh:+ docker = (tomllib.load(fh).get("openshell") or {}).get("drivers", {}).get("docker") or {}+ except (OSError, tomllib.TOMLDecodeError, AttributeError):return False+ admission = docker.get("resource_admission") or {}+ return docker.get("allow_driver_config") is True and docker.get("enable_bind_mounts") is True and admission.get("enabled") is Falsedef gateway_config_in_use() -> Optional[Path]:diff --git a/tests/test_sandbox_setup_job.py b/tests/test_sandbox_setup_job.pyindex ea0687e7..865561c4 100644--- a/tests/test_sandbox_setup_job.py+++ b/tests/test_sandbox_setup_job.py@@ -257,7 +257,9 @@ def test_the_folder_step_restarts_the_gateway_and_the_check_reads_the_config_in_assert setup_cmd.apply_bind_mounts() is Noneassert ["brew", "services", "restart", "nvidia/openshell/openshell"] in ranours = tmp_path / "openshell" / "gateway.toml"- assert "enable_bind_mounts = true" in ours.read_text()+ written = ours.read_text()+ assert "version = 2" in written and "allow_driver_config = true" in written and "enable_bind_mounts = true" in written+ assert "[openshell.drivers.docker.resource_admission]\nenabled = false" in written# Restarted: no --config, so `gateway.env` names the file, and the check is satisfied.listing["out"] = "/opt/homebrew/opt/openshell/bin/openshell-gateway\n"assert setup_cmd.gateway_config_in_use() == ours and setup_cmd._allows_bind_mounts(ours)@@ -304,3 +306,29 @@ def test_on_a_mac_the_docker_kernel_is_asked_for_landlock(monkeypatch):row = {s.key: s for s in setup_cmd.steps()}["docker_landlock"]assert keys[:2] == ["docker", "docker_landlock"] and not row.ok and not row.fixableassert row.hint.startswith("Update Docker Desktop") and row.docs == setup_cmd.DOCKER_DESKTOP_URL+++def test_folder_sharing_needs_all_three_gateway_settings(tmp_path):+ """OpenShell 0.1: driver config on, bind mounts on, resource admission off."""+ config = tmp_path / "gateway.toml"+ config.write_text(setup_cmd._BIND_MOUNTS)+ assert setup_cmd._allows_bind_mounts(config)+ for missing in ("allow_driver_config = true\n", "enable_bind_mounts = true\n", "enabled = false\n"):+ config.write_text(setup_cmd._BIND_MOUNTS.replace(missing, ""))+ assert not setup_cmd._allows_bind_mounts(config), missing+ # The file our setup wrote for OpenShell 0.0.x is not enough, and is not valid for 0.1.+ config.write_text("[openshell.drivers.docker]\nenable_bind_mounts = true\n")+ assert not setup_cmd._allows_bind_mounts(config)+ config.write_text("not toml [")+ assert not setup_cmd._allows_bind_mounts(config)+++def test_a_gateway_config_with_other_settings_is_not_rewritten(tmp_path, monkeypatch):+ monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))+ theirs = tmp_path / "openshell" / "gateway.toml"+ theirs.parent.mkdir(parents=True)+ theirs.write_text("[openshell]\nversion = 2\n\n[openshell.gateway]\ncompute_driver = \"docker\"\n")+ said = setup_cmd.apply_bind_mounts()+ assert said and "by hand" in said and "allow_driver_config = true" in said+ assert "compute_driver" in theirs.read_text() and "enable_bind_mounts" not in theirs.read_text()+