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
fix(studio): mark /api reads no-store so an idle desktop app stops rewriting its disk cache (#12148)
alkinun1dddc1437d9e
diff --git a/studio/backend/main.py b/studio/backend/main.pyindex cb9beba83f5..c8398e4babf 100644--- a/studio/backend/main.py+++ b/studio/backend/main.py@@ -1159,6 +1159,10 @@ def _build_csp(script_nonce: "str | None" = None, *, docs: bool = False) -> str:)+# Any of these means the response already says how a browser may cache or revalidate it.+_CACHE_POLICY_HEADERS = ("cache-control", "expires", "etag", "last-modified")++class SecurityHeadersMiddleware:"""Set baseline security headers; splice per-response inline-script nonces into CSP. Pure ASGI (notBaseHTTPMiddleware) so streaming responses are not wrapped in an anyio stream."""@@ -1197,6 +1201,14 @@ class SecurityHeadersMiddleware:"Permissions-Policy","camera=(), microphone=(self), geolocation=()",)+ # An API read with no cache policy and no validators can never be reused, yet Chromium and+ # WebView2 still write each one to the disk cache. The UI polls several for as long as it is+ # open, and the API monitor returns its whole history each time: ~200 KB of disk writes every+ # 2 s from an idle desktop app. Routes with their own policy or validators keep them.+ if path.startswith("/api/") and not any(+ name in headers for name in _CACHE_POLICY_HEADERS+ ):+ headers["Cache-Control"] = "no-store"headers["server"] = "unsloth-studio"await send(message)diff --git a/studio/backend/tests/test_middleware.py b/studio/backend/tests/test_middleware.pyindex 1db76b2e8b0..ab576c8327a 100644--- a/studio/backend/tests/test_middleware.py+++ b/studio/backend/tests/test_middleware.py@@ -13,7 +13,7 @@ from pathlib import Pathimport pytestfrom fastapi import FastAPI, HTTPException, Request-from fastapi.responses import Response+from fastapi.responses import FileResponse, Responsefrom fastapi.testclient import TestClientfrom starlette.middleware.gzip import GZipMiddleware@@ -518,6 +518,29 @@ def _make_csp_app(main_module, attach_nonce: str | None = None):return app+def _make_api_cache_app(main_module, file_path: Path | None = None):+ app = FastAPI()+ app.add_middleware(main_module.SecurityHeadersMiddleware)++ @app.get("/api/inference/monitor")+ async def monitor():+ return {"entries": []}++ @app.get("/api/video/asset")+ async def asset():+ return Response(+ content = b"mp4",+ media_type = "video/mp4",+ headers = {"Cache-Control": "private, max-age=31536000, immutable"},+ )++ @app.get("/api/file")+ async def file():+ return FileResponse(file_path)++ return app++class TestSecurityHeadersMiddleware:def test_csp_has_no_unsafe_inline_for_script_src(self, main_module):app = _make_csp_app(main_module)@@ -549,6 +572,33 @@ class TestSecurityHeadersMiddleware:assert "geolocation=()" in permissions_policyassert r.headers["server"] == "unsloth-studio"+ def test_api_read_without_cache_policy_is_no_store(self, main_module):+ # Polled JSON like the API monitor: Chromium/WebView2 would write every poll to its disk cache.+ app = _make_api_cache_app(main_module)+ r = TestClient(app).get("/api/inference/monitor")+ assert r.status_code == 200+ assert r.headers["cache-control"] == "no-store"++ def test_api_route_cache_policy_is_kept(self, main_module):+ app = _make_api_cache_app(main_module)+ r = TestClient(app).get("/api/video/asset")+ assert r.headers["cache-control"] == "private, max-age=31536000, immutable"++ def test_api_file_with_validators_stays_revalidatable(self, main_module, tmp_path):+ # FileResponse carries ETag/Last-Modified, so the browser can revalidate instead of refetching.+ path = tmp_path / "out.png"+ path.write_bytes(b"png")+ app = _make_api_cache_app(main_module, file_path = path)+ r = TestClient(app).get("/api/file")+ assert r.status_code == 200+ assert "etag" in r.headers+ assert "cache-control" not in r.headers++ def test_non_api_response_gets_no_cache_policy(self, main_module):+ app = _make_csp_app(main_module)+ r = TestClient(app).get("/plain")+ assert "cache-control" not in r.headers+def test_mirror_endpoints_in_connect_src(self, main_module, monkeypatch):# A mirror must reach connect-src or the browser blocks the Hub calls.monkeypatch.setenv("HF_ENDPOINT", "https://hf-mirror.com")