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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: approval waits free their slot, Deep Research honours --disable-tools, first-run shutdown survives a read error (#14434)
Daniel Hanf72dd1e5de37
diff --git a/studio/backend/auth/bootstrap_timeout.py b/studio/backend/auth/bootstrap_timeout.pyindex 5b6ee49e503..c7ff816ef83 100644--- a/studio/backend/auth/bootstrap_timeout.py+++ b/studio/backend/auth/bootstrap_timeout.py@@ -21,6 +21,9 @@ from typing import OptionalBOOTSTRAP_TIMEOUT_ENV_VAR = "UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT"DEFAULT_BOOTSTRAP_TIMEOUT_SECONDS = 3600+# Retry cadence and window for a failed auth storage read at the deadline.+STORAGE_RETRY_SECONDS = 5+STORAGE_RETRY_WINDOW_SECONDS = 600# Monotonic expiry, or None when nothing is armed. Read back over HTTP because the# stderr warning is lost on the launches that arm it (--secure, external bind).@@ -118,20 +121,37 @@ def enforce_bootstrap_password_deadline() -> bool:"""Deadline handler: shut down iff the seeded admin password is still unchanged. Returns True if itshut Unsloth down, False if it left it running (the password was changed in time)."""- try:- still_default = storage.requires_password_change(storage.DEFAULT_ADMIN_USERNAME)- except Exception:- return False- if not still_default:+ # One-shot timer: a failed read retries for a bounded window, then counts as unsecured.+ give_up_at = time.monotonic() + STORAGE_RETRY_WINDOW_SECONDS+ while True:+ try:+ still_default = storage.requires_password_change(storage.DEFAULT_ADMIN_USERNAME)+ break+ except Exception as e:+ if logger is not None:+ logger.warning("Bootstrap deadline could not read auth storage, retrying: %s", e)+ if time.monotonic() >= give_up_at:+ still_default = None+ break+ time.sleep(STORAGE_RETRY_SECONDS)+ if still_default is not None and not still_default:return False- message = (- "\nUnsloth Studio was exposed on the network but its default admin "- f"password was not changed within {_format_duration(timeout_seconds)}. "- "Shutting down to avoid leaving an unsecured public instance running.\n"- "Next time, sign in and change the password on first login, or set "- f"{BOOTSTRAP_TIMEOUT_ENV_VAR}=0 to disable this timeout."- )+ if still_default is None:+ message = (+ "\nUnsloth Studio was exposed on the network and auth storage stayed unreadable for "+ f"{_format_duration(STORAGE_RETRY_WINDOW_SECONDS)} after the first-login deadline, so "+ "whether the default admin password was changed is unknown. "+ "Shutting down to avoid leaving an unsecured public instance running."+ )+ else:+ message = (+ "\nUnsloth Studio was exposed on the network but its default admin "+ f"password was not changed within {_format_duration(timeout_seconds)}. "+ "Shutting down to avoid leaving an unsecured public instance running.\n"+ "Next time, sign in and change the password on first login, or set "+ f"{BOOTSTRAP_TIMEOUT_ENV_VAR}=0 to disable this timeout."+ )if logger is not None:logger.warning(message)print(message, file = sys.stderr, flush = True)diff --git a/studio/backend/core/research_runs.py b/studio/backend/core/research_runs.pyindex 33c93543c73..0a32b58c182 100644--- a/studio/backend/core/research_runs.py+++ b/studio/backend/core/research_runs.py@@ -66,6 +66,7 @@ from core.research.prompts import (_system_prompt_with_instructions,)from loggers import get_logger+from state.tool_policy import get_tool_policyfrom storage import providers_dbfrom storage import research_runs_db as dbfrom storage.studio_db import (@@ -2404,6 +2405,10 @@ class ResearchSupervisor:# The structured inline card renders the plan; no second markdown copy below it.async def _research(self, run: dict) -> None:+ # The routes refuse to start a run while tools are off; a durable run approved before a+ # restart under `--disable-tools` is still claimed here.+ if get_tool_policy() is False:+ raise RuntimeError("Tools are disabled on this server")# Completed steps on a queued run are ones a Retry kept (a fresh plan's steps are all pending).resuming = run.get("claimedFromStatus") == "running" or any(step.get("status") == "completed" for step in run.get("steps") or ()diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 541257acafb..8ecc50e1657 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -34134,9 +34134,10 @@ async def produce_openai_chat_completions(_tool_decode_finished = Truebreak- # Anything after the gated tool_start means the user answered.+ # Anything after the gated tool_start is the answer, except the context meter sent before the wait.if not ((event["type"] == "tool_start" and event.get("awaiting_confirmation"))+ or event["type"] == "context_usage"or (event["type"] == "skill_load"and event.get("status") == "awaiting_approval"diff --git a/studio/backend/routes/research_runs.py b/studio/backend/routes/research_runs.pyindex 54960c59742..dfebf8e121d 100644--- a/studio/backend/routes/research_runs.py+++ b/studio/backend/routes/research_runs.py@@ -146,6 +146,13 @@ def _require_run(run_id: str) -> dict:return run+def _require_tools_enabled() -> None:+ """A run searches the web and the knowledge base from the supervisor's task, outside this+ request, so the caller's server-side tool policy is applied before a run is set going."""+ if get_tool_policy() is False:+ raise HTTPException(status_code = 403, detail = "Tools are disabled on this server")++def _sync_assistant(run: dict, text: str | None = None) -> None:message_id = db.discover_and_bind_assistant_message(run["id"])if not message_id:@@ -510,6 +517,7 @@ def create_research_run(request: Request,current_subject: str = Depends(get_current_subject),):+ _require_tools_enabled()thread = get_chat_thread(payload.threadId)if thread is None:raise HTTPException(status_code = 404, detail = "Thread not found")@@ -606,6 +614,7 @@ def approve_research_plan(request: Request,current_subject: str = Depends(get_current_subject),):+ _require_tools_enabled()_require_run(run_id)try:db.approve(run_id, payload.planRevision, payload.planHash)@@ -643,6 +652,7 @@ def retry_research_run(current_subject: str = Depends(get_current_subject),payload: RetryResearchRun | None = None,):+ _require_tools_enabled()_require_run(run_id)model_timeout = Nonewebsite_policy = Nonediff --git a/studio/backend/tests/test_bootstrap_timeout.py b/studio/backend/tests/test_bootstrap_timeout.pyindex d9d09cf8739..8c35a92ec8d 100644--- a/studio/backend/tests/test_bootstrap_timeout.py+++ b/studio/backend/tests/test_bootstrap_timeout.py@@ -237,3 +237,57 @@ def test_arming_publishes_the_deadline():finally:timer.cancel()clear_bootstrap_deadline()+++def _flaky_storage(failures: int, requires_change: bool):+ reads = []++ def _requires_password_change(_username):+ reads.append(1)+ if len(reads) <= failures:+ raise OSError("unable to open database file")+ return requires_change++ return SimpleNamespace(+ DEFAULT_ADMIN_USERNAME = "unsloth",+ requires_password_change = _requires_password_change,+ )+++def test_deadline_retries_an_unreadable_storage(monkeypatch):+ # A failed read is not "changed": the timer is one-shot, so giving up would drop the deadline for good.+ monkeypatch.setattr("auth.bootstrap_timeout.STORAGE_RETRY_SECONDS", 0)+ calls = []+ result = enforce_bootstrap_password_deadline(+ _flaky_storage(failures = 2, requires_change = True),+ lambda: calls.append("shutdown"),+ timeout_seconds = 3600,+ )+ assert result is True+ assert calls == ["shutdown"]+++def test_unreadable_storage_does_not_stop_a_changed_password(monkeypatch):+ monkeypatch.setattr("auth.bootstrap_timeout.STORAGE_RETRY_SECONDS", 0)+ calls = []+ result = enforce_bootstrap_password_deadline(+ _flaky_storage(failures = 2, requires_change = False),+ lambda: calls.append("shutdown"),+ timeout_seconds = 3600,+ )+ assert result is False+ assert calls == []+++def test_storage_unreadable_for_the_whole_window_fails_closed(monkeypatch):+ # Unknown for the whole retry window is treated as unsecured, not as "changed".+ monkeypatch.setattr("auth.bootstrap_timeout.STORAGE_RETRY_SECONDS", 0)+ monkeypatch.setattr("auth.bootstrap_timeout.STORAGE_RETRY_WINDOW_SECONDS", 0)+ calls = []+ result = enforce_bootstrap_password_deadline(+ _flaky_storage(failures = 10_000, requires_change = False),+ lambda: calls.append("shutdown"),+ timeout_seconds = 3600,+ )+ assert result is True+ assert calls == ["shutdown"]diff --git a/studio/backend/tests/test_research_runs_storage.py b/studio/backend/tests/test_research_runs_storage.pyindex f71bf619382..577a6f9d5aa 100644--- a/studio/backend/tests/test_research_runs_storage.py+++ b/studio/backend/tests/test_research_runs_storage.py@@ -4712,3 +4712,80 @@ def test_planner_opt_out_is_only_sent_where_the_model_has_one(research_home, monexternal["providerType"] = "mistral"mistral = planner_payload(supportsReasoning = False, supportsReasoningOff = False)assert "enable_thinking" not in mistral and "reasoning_effort" not in mistral++[email protected](params = ["--disable-tools", "keyless caller without the tool grant"])+def tools_off(request):+ from state import tool_policy+ if request.param == "--disable-tools":+ tool_policy.set_tool_policy(False)+ yield+ tool_policy.reset_tool_policy()+ else:+ with tool_policy.tools_force_disabled():+ yield+++def test_no_run_is_set_going_while_tools_are_off(research_home, tools_off):+ """The worker runs web_search and search_knowledge_base outside the request, so the caller's+ tool policy is applied by the routes that hand it a run."""+ from fastapi import HTTPException+ from routes import research_runs as research_route++ request = SimpleNamespace(app = SimpleNamespace(state = SimpleNamespace()))++ def refused(route, *args):+ with pytest.raises(HTTPException) as exc:+ route(*args, request, current_subject = "alice")+ return (exc.value.status_code, exc.value.detail) == (+ 403,+ "Tools are disabled on this server",+ )++ assert refused(+ research_route.create_research_run,+ research_route.CreateResearchRun(+ threadId = "thread-1",+ userMessageId = "user-1",+ inferenceRequest = {"model": "local-model"},+ ),+ )+ assert not research_db.has_thread_claim("thread-1")++ _create()+ plan = research_db.set_plan("run-1", _plan())+ approval = research_route.ApprovePlan(+ planRevision = plan["planRevision"], planHash = plan["planHash"]+ )+ assert refused(research_route.approve_research_plan, "run-1", approval)+ assert research_db.get_run("run-1")["status"] == "awaiting_approval"++ # Stopping a run uses no tool.+ assert research_route.cancel_research_run("run-1", request, "alice")["status"] == "cancelled"+ assert refused(research_route.retry_research_run, "run-1")+ assert research_db.get_run("run-1")["status"] == "cancelled"+++def test_worker_leaves_an_approved_run_unsearched_under_disable_tools(research_home, monkeypatch):+ """A durable run outlives a restart, so one approved earlier is claimed under the new policy."""+ from state import tool_policy++ worker, supervisor = _bare_supervisor(monkeypatch)+ _create(rag_scope = {"kind": "thread", "id": "thread-1"})+ plan = research_db.set_plan("run-1", _plan())+ research_db.approve("run-1", plan["planRevision"], plan["planHash"])+ called = []++ async def undecided(*args, **kwargs):+ # Not a valid action, so each step falls back to the plan's next query.+ return "noise", "", "stop", None++ monkeypatch.setattr(supervisor, "_stream_completion", undecided)+ monkeypatch.setattr(worker, "execute_tool", lambda name, *a, **k: called.append(name) or "")+ monkeypatch.setattr(tool_policy, "_tool_policy", False)++ asyncio.run(supervisor._process(research_db.claim_next(supervisor.worker_id)))++ run = research_db.get_run("run-1")+ assert called == []+ assert (run["status"], run["error"]) == ("failed", "Tools are disabled on this server")diff --git a/studio/backend/tests/test_tool_approval_park_context_usage.py b/studio/backend/tests/test_tool_approval_park_context_usage.pynew file mode 100644index 00000000000..a91cfdbdd41--- /dev/null+++ b/studio/backend/tests/test_tool_approval_park_context_usage.py@@ -0,0 +1,122 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""A GGUF run waiting on a tool approval keeps its llama-server slot parked.++The tool loop sends a ``context_usage`` meter frame between the gated ``tool_start`` and the wait+for the user's decision. That frame is not the answer: the slot must stay parked until a real+answer (``status`` / ``tool_end``) arrives, or every open approval prompt holds a slot.+"""++import pytest+from fastapi import FastAPI+from fastapi.testclient import TestClient++from auth.authentication import get_current_subject+from core.inference import llama_admission+from core.inference.llama_admission import LlamaAdmissionLease+import routes.inference as inference_route+from .llama_backend_double import FakeLlamaCppBackend+++def lease_log(monkeypatch):+ log = []+ real_park = LlamaAdmissionLease.park+ real_unpark_async = LlamaAdmissionLease.unpark_async+ real_unpark = LlamaAdmissionLease.unpark++ def park(self):+ ok = real_park(self)+ log.append("park" if ok else "park-refused")+ return ok++ async def unpark_async(self, *args, **kwargs):+ log.append("unpark")+ return await real_unpark_async(self, *args, **kwargs)++ def unpark(self):+ log.append("unpark")+ return real_unpark(self)++ # Park budget is sized from the host's executor; pin it so the test does not depend on CPU count.+ monkeypatch.setattr(llama_admission, "_max_parked", lambda capacity: 4)+ monkeypatch.setattr(LlamaAdmissionLease, "park", park)+ monkeypatch.setattr(LlamaAdmissionLease, "unpark_async", unpark_async)+ monkeypatch.setattr(LlamaAdmissionLease, "unpark", unpark)+ return log+++def _client(monkeypatch, log):+ class _Backend(FakeLlamaCppBackend):+ supports_tools = True+ context_length = 8192+ base_url = "http://approval-park-context-usage"+ effective_parallel_slots = 2++ def count_chat_tokens(self, *args, **kwargs):+ return 20++ def _request_reasoning_kwargs(self, *args, **kwargs):+ return {}++ def generate_chat_completion_with_tools(self, **kwargs):+ yield {+ "type": "tool_start",+ "tool_name": "web_search",+ "tool_call_id": "call_1",+ "arguments": {"query": "x"},+ "awaiting_confirmation": True,+ }+ yield {+ "type": "context_usage",+ "prompt_tokens": 12,+ "completion_tokens": 3,+ "context_length": 8192,+ }+ # The loop now blocks on the user's decision.+ log.append("waiting-for-decision")+ yield {"type": "status", "text": "Searching"}+ yield {+ "type": "tool_end",+ "tool_name": "web_search",+ "tool_call_id": "call_1",+ "result": "r",+ }+ yield {"type": "content", "text": "done"}+ yield {"type": "metadata", "finish_reason": "stop"}++ monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: _Backend())+ monkeypatch.setattr(inference_route, "_effective_enable_tools", lambda payload: True)++ async def _select(payload, **_kwargs):+ return [{"type": "function", "function": {"name": "web_search"}}]++ monkeypatch.setattr(inference_route, "_select_request_tools", _select)+ app = FastAPI()+ app.include_router(inference_route.router)+ app.dependency_overrides[get_current_subject] = lambda: "test-user"+ return TestClient(app)+++def test_context_meter_before_the_wait_does_not_take_the_slot_back(monkeypatch, lease_log):+ response = _client(monkeypatch, lease_log).post(+ "/chat/completions",+ json = {+ "messages": [{"role": "user", "content": "search it"}],+ "stream": True,+ "enable_tools": True,+ },+ headers = {"X-Unsloth-Events": "1"},+ )+ assert response.status_code == 200, response.text+ assert "context_usage" in response.text, "the meter frame must still reach the UI"+ assert "park" in lease_log, lease_log+ wait = lease_log.index("waiting-for-decision")+ park = lease_log.index("park")+ assert park < wait, lease_log+ assert "unpark" not in lease_log[park:wait], (+ "the slot was taken back before the user answered: " + repr(lease_log)+ )+ # The answer (status / tool_end) takes the slot back.+ assert "unpark" in lease_log[wait:], lease_log