Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Avoid duplicate successful sandbox probes during refresh (#16735)

Nathan Tran280de8a9cd4c
diff --git a/studio/backend/routes/sandbox_capability.py b/studio/backend/routes/sandbox_capability.py
index 25b98e72414..d68f1ae2eb4 100644
--- a/studio/backend/routes/sandbox_capability.py
+++ b/studio/backend/routes/sandbox_capability.py
@@ -41,11 +41,16 @@ def _setup_fields_for(request: Request, isolated: bool) -> dict:
def _refresh(force: bool) -> None:
from core.inference import os_sandbox
+
if os_sandbox._background_probes_disabled():
return # UNSLOTH_DISABLE_SANDBOX_WARMUP=1: answers come only from real launches
+ probe_force = force
for tool in os_sandbox.ISOLATED_TOOLS:
if force or not os_sandbox.has_tool_isolation_answer(tool):
- os_sandbox.refresh_tool_isolation(tool, force = force)
+ available = os_sandbox.refresh_tool_isolation(tool, force = probe_force)
+ if available and sys.platform in ("linux", "darwin"):
+ # POSIX tools share a probe. An undecided check can leave an older cached pass.
+ probe_force = False
def _capability() -> dict:
diff --git a/studio/backend/routes/settings.py b/studio/backend/routes/settings.py
index e74042ffde6..91a9a003eb2 100644
--- a/studio/backend/routes/settings.py
+++ b/studio/backend/routes/settings.py
@@ -5613,8 +5613,12 @@ def _build_sandbox_status(force: bool) -> SandboxStatusResponse:
)
terminal_exe, shell = _sandbox_terminal_target()
# On Windows the choice above just probed this executable.
+ terminal_force = force and not windows_refresh
+ if sys.platform in ("linux", "darwin") and python.available:
+ # Reuse a successful POSIX probe; an undecided check can leave an older cached pass.
+ terminal_force = False
terminal = os_sandbox.capability_snapshot(
- force = force and not windows_refresh,
+ force = terminal_force,
execution_kind = "terminal",
selected_executable = terminal_exe,
)
diff --git a/studio/backend/tests/test_permission_mode_degrade.py b/studio/backend/tests/test_permission_mode_degrade.py
index 1d597c8a6a1..897dcde8d44 100644
--- a/studio/backend/tests/test_permission_mode_degrade.py
+++ b/studio/backend/tests/test_permission_mode_degrade.py
@@ -362,6 +362,100 @@ class _Backend:
BACKEND_NAME = "fake"
+ params = list(itertools.product(("linux", "darwin"), ("settings", "capability"))),
+ ids = lambda case: "-".join(case),
+)
+def posix_refresh(request, monkeypatch, probe_env):
+ from core.inference import sandbox_linux
+ from routes import sandbox_capability, settings
+
+ platform, route = request.param
+ monkeypatch.setattr(sys, "platform", platform)
+ monkeypatch.setattr(sandbox_linux, "empty_proc_layout", lambda: False)
+ monkeypatch.setattr(os_sandbox, "_host_key", lambda: "host")
+ monkeypatch.setattr(os_sandbox, "linux_unavailable_remediation", lambda: "blocked")
+ monkeypatch.setattr(settings, "_sandbox_terminal_target", lambda: ("/bin/bash", None))
+ monkeypatch.setattr(settings, "_sandbox_setup_status", lambda _available: None)
+ return settings._build_sandbox_status if route == "settings" else sandbox_capability._refresh
+
+
+def test_a_successful_refresh_probes_once_and_refreshes_both_tools(posix_refresh, probe_env):
+ _identity, verdicts, _gate, calls = probe_env
+ for tool in os_sandbox.ISOLATED_TOOLS:
+ os_sandbox.note_tool_isolation(tool, False, backend = "none", reason = "old failure")
+ verdicts.extend((True, f"fresh {i}") for i in range(3))
+
+ for i in range(3):
+ posix_refresh(True)
+ assert len(calls) == i + 1
+ for tool in os_sandbox.ISOLATED_TOOLS:
+ answer = os_sandbox.cached_tool_capability(tool)
+ assert answer[0] is True and answer[2] == f"fresh {i}"
+ posix_refresh(False)
+ assert len(calls) == i + 1
+
+
[email protected]("reason", ["blocked", sandbox_probe.UndecidedReason("out of disk")])
+def test_a_failed_refresh_retries_even_after_a_cached_pass(
+ posix_refresh, probe_env, monkeypatch, reason
+):
+ _identity, verdicts, _gate, calls = probe_env
+ verdicts.append((True, "old pass"))
+ assert os_sandbox.capability_snapshot(force = True).available
+ calls.clear()
+ verdicts.extend([(False, reason), (False, "still blocked")])
+
+ posix_refresh(True)
+
+ assert len(calls) == 2
+ # Reading an undecided answer would otherwise start an unrelated background retry.
+ monkeypatch.setenv(os_sandbox.WARMUP_DISABLE_ENV, "1")
+ assert os_sandbox.cached_tool_capability("python") == (False, "none", reason)
+ assert os_sandbox.cached_tool_capability("terminal") == (False, "none", "still blocked")
+
+
[email protected]("change", ["reset", "runtime"])
+def test_a_refresh_rechecks_when_the_cache_changes_between_tools(
+ posix_refresh, probe_env, monkeypatch, change
+):
+ identity, verdicts, _gate, calls = probe_env
+ verdicts.extend([(True, "before change"), (False, "after change")])
+ real_snapshot = os_sandbox.capability_snapshot
+ answers = []
+
+ def snapshot(**kwargs):
+ if kwargs["execution_kind"] == "terminal":
+ if change == "reset":
+ sandbox_probe.reset_probe_cache()
+ else:
+ identity[0] = "new runtime"
+ answer = real_snapshot(**kwargs)
+ answers.append(answer.available)
+ return answer
+
+ monkeypatch.setattr(os_sandbox, "capability_snapshot", snapshot)
+ posix_refresh(True)
+
+ assert len(calls) == 2
+ assert answers == [True, False]
+
+
+def test_windows_capability_refresh_keeps_forcing_each_tool(monkeypatch, probe_env):
+ from routes import sandbox_capability
+
+ monkeypatch.setattr(sys, "platform", "win32")
+ calls = []
+
+ def refresh(tool, *, force = False):
+ calls.append((tool, force))
+ return True
+
+ monkeypatch.setattr(os_sandbox, "refresh_tool_isolation", refresh)
+ sandbox_capability._refresh(True)
+ assert calls == [("python", True), ("terminal", True)]
+
+
def _wait_for(predicate, timeout = 5.0):
deadline = time.time() + timeout
while not predicate() and time.time() < deadline:
diff --git a/studio/backend/tests/test_sandbox_setup_route.py b/studio/backend/tests/test_sandbox_setup_route.py
index fde9d9f860a..6dc777b595b 100644
--- a/studio/backend/tests/test_sandbox_setup_route.py
+++ b/studio/backend/tests/test_sandbox_setup_route.py
@@ -318,7 +318,7 @@ def test_the_capability_read_checks_a_tool_it_has_no_answer_for(host, linux, mon
_capability_client(OWNER).get("/api/sandbox/capability")
assert refreshed == []
_capability_client(OWNER).get("/api/sandbox/capability", params = {"refresh": "1"})
- assert refreshed == [("python", True), ("terminal", True)]
+ assert refreshed == [("python", True), ("terminal", False)]
refreshed.clear()
_capability_client(ALICE).get("/api/sandbox/capability", params = {"refresh": "1"})
assert refreshed == []