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: give the liveness polls one shared heartbeat instead of one each (#8310)
Daniel Han64f6ed5f62c1
diff --git a/studio/backend/loggers/handlers.py b/studio/backend/loggers/handlers.pyindex b05ab7fcb92..ab28222cf33 100644--- a/studio/backend/loggers/handlers.py+++ b/studio/backend/loggers/handlers.py@@ -76,6 +76,33 @@ _QUIET_POLL_PATHS = {"/api/inference/images/load-progress","/api/inference/video/load-progress",}+# The pure-liveness subset of _QUIET_POLL_PATHS. Every one of these answers the same+# question ("the server is up and answering"), and the SPA fires them together in one+# burst, so heartbeating them independently emits one line per path per window instead+# of one line per window. They share a single bucket: the first of the burst logs with+# its real path, the rest of that window is dropped. Measured over four Studio sessions+# these were 39-69% of the access log and the shared bucket removed 25-47% of it.+#+# Only this group is shared. The other _QUIET_POLL_PATHS entries (/api/train/runs,+# /api/models/checkpoints, /api/models/local, /api/rag/knowledge-bases, the download+# polls) each report on a different subsystem, so they keep their own heartbeat.+# Two paths are deliberately NOT here because their latency is worth seeing on its+# own rather than being stamped out by a cheap sibling: /api/health (main.py waits up+# to a second for hardware detection, and the desktop preflight has a two-second+# deadline) and /api/inference/status (its handler reads llama.cpp capabilities and+# runs a release-freshness check in an executor).+_LIVENESS_POLL_PATHS = frozenset(+ {+ "/api/auth/status",+ "/api/inference/monitor",+ "/api/inference/images/status",+ "/api/inference/video/status",+ "/api/inference/audio/stt/status",+ }+)+# Bucket key for the group above. Not a real (method, path, query, status), so it can+# never collide with one.+_LIVENESS_DEDUP_KEY = ("GET", "\x00liveness", b"", 200)_DEDUP_MAP_MAX = 4096_NATIVE_PATH_LEASE_RE = re.compile(r"(?i)(\b(?:native_path_lease|nativePathLease)[\"']?\s*[:=]\s*[\"']?)[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+"@@ -219,10 +246,21 @@ class LoggingMiddleware:heartbeat. Stamps only on emit, so steady polls still log."""if method != "GET" or not (200 <= status_code < 300):return False- window_ms = _QUIET_POLL_DEDUP_MS if path in _QUIET_POLL_PATHS else _ACCESS_LOG_DEDUP_MS+ # A query makes the request something other than the background poll:+ # /api/inference/audio/stt/status?model=... extends the downloaded check to a+ # custom repo, so it keeps its own identity rather than joining the bucket.+ is_liveness = path in _LIVENESS_POLL_PATHS and not query+ window_ms = (+ _QUIET_POLL_DEDUP_MS+ if is_liveness or path in _QUIET_POLL_PATHS+ else _ACCESS_LOG_DEDUP_MS+ )if window_ms <= 0:return False- key = (method, path, query, status_code)+ # The liveness group shares one bucket, so a burst of them logs once, not once+ # per path. Only the query-less form joins it, so a parameterized call still+ # gets its own status and latency line.+ key = _LIVENESS_DEDUP_KEY if is_liveness else (method, path, query, status_code)last = self._last_log.get(key)if last is not None and (now - last) * 1000.0 < window_ms:return Truediff --git a/studio/backend/tests/test_logging_middleware.py b/studio/backend/tests/test_logging_middleware.pyindex 0d873e6e54b..c5a7e598f97 100644--- a/studio/backend/tests/test_logging_middleware.py+++ b/studio/backend/tests/test_logging_middleware.py@@ -418,17 +418,18 @@ def test_unrelated_image_routes_still_log(logs, monkeypatch):]-def test_indicator_status_polls_collapse_to_a_heartbeat(logs, monkeypatch):- # The loaded-models indicator reads all four runtimes every 5s for as long- # as the app is open, and on the desktop every line is mirrored into- # tauri.log. /api/inference/status was already quiet; its three siblings- # arrived with the indicator and have to be quiet for the same reason.+def test_indicator_status_polls_collapse_to_one_shared_heartbeat(logs, monkeypatch):+ # The loaded-models indicator reads all four runtimes every 5s for as long as the+ # app is open, and on the desktop every line is mirrored into tauri.log. The three+ # cheap ones answer the same question, so they share one heartbeat bucket: one line+ # per window in total, not one per path. Previously each path heartbeated+ # separately, which still meant a line per path per window. /api/inference/status+ # is excluded on purpose (its handler can be slow), and is covered by its own test.monkeypatch.setattr(hmod, "_ACCESS_LOG_DEDUP_MS", 0)monkeypatch.setattr(hmod, "_QUIET_POLL_DEDUP_MS", 1000)mw = LoggingMiddleware(_status_app(200))polled = (- "/api/inference/status","/api/inference/images/status","/api/inference/video/status","/api/inference/audio/stt/status",@@ -438,10 +439,130 @@ def test_indicator_status_polls_collapse_to_a_heartbeat(logs, monkeypatch):_run(mw(_http_scope(path), _noop_receive, _drop))paths = _paths_logged(logs)- for path in polled:+ assert len(paths) == 1, paths+ assert paths[0] in polled+++def test_the_runtime_status_polls_share_the_liveness_bucket(logs, monkeypatch):+ # /api/auth/status and the inference status polls are the same "still up" signal,+ # so they must not each add a line of their own.+ monkeypatch.setattr(hmod, "_ACCESS_LOG_DEDUP_MS", 0)+ monkeypatch.setattr(hmod, "_QUIET_POLL_DEDUP_MS", 1000)++ mw = LoggingMiddleware(_status_app(200))+ for path in ("/api/auth/status", "/api/inference/monitor", "/api/inference/images/status"):+ _run(mw(_http_scope(path), _noop_receive, _drop))++ assert len(_paths_logged(logs)) == 1, _paths_logged(logs)+++def test_health_keeps_its_own_heartbeat(logs, monkeypatch):+ # main.py waits up to a second for hardware detection and the desktop preflight+ # has a two-second deadline, so a slow-but-successful /api/health is exactly the+ # line worth keeping; it must not be suppressed by a cheap status poll.+ monkeypatch.setattr(hmod, "_ACCESS_LOG_DEDUP_MS", 0)+ monkeypatch.setattr(hmod, "_QUIET_POLL_DEDUP_MS", 1000)++ mw = LoggingMiddleware(_status_app(200))+ _run(mw(_http_scope("/api/inference/monitor"), _noop_receive, _drop))+ _run(mw(_http_scope("/api/health"), _noop_receive, _drop))++ assert len(_paths_logged(logs)) == 2, _paths_logged(logs)+++def test_the_slow_inference_probe_keeps_its_own_heartbeat(logs, monkeypatch):+ # get_status reads llama.cpp capabilities and checks release freshness in an+ # executor, so a slow but successful probe is worth its own process_time_ms.+ monkeypatch.setattr(hmod, "_ACCESS_LOG_DEDUP_MS", 0)+ monkeypatch.setattr(hmod, "_QUIET_POLL_DEDUP_MS", 1000)++ mw = LoggingMiddleware(_status_app(200))+ _run(mw(_http_scope("/api/inference/images/status"), _noop_receive, _drop))+ _run(mw(_http_scope("/api/inference/status"), _noop_receive, _drop))++ assert len(_paths_logged(logs)) == 2, _paths_logged(logs)+++def test_a_parameterized_stt_status_keeps_its_own_line(logs, monkeypatch):+ # fetchSttStatus(refreshKey, model) asks whether a custom repo is downloaded,+ # which is not the background "still up" poll and must not be swallowed by it.+ monkeypatch.setattr(hmod, "_ACCESS_LOG_DEDUP_MS", 0)+ monkeypatch.setattr(hmod, "_QUIET_POLL_DEDUP_MS", 1000)++ mw = LoggingMiddleware(_status_app(200))+ _run(mw(_http_scope("/api/health"), _noop_receive, _drop))+ scope = _http_scope("/api/inference/audio/stt/status")+ scope["query_string"] = b"model=acme%2Fwhisper-custom"+ _run(mw(scope, _noop_receive, _drop))++ assert len(_paths_logged(logs)) == 2, _paths_logged(logs)+++def test_two_different_stt_models_do_not_collapse(logs, monkeypatch):+ monkeypatch.setattr(hmod, "_ACCESS_LOG_DEDUP_MS", 0)+ monkeypatch.setattr(hmod, "_QUIET_POLL_DEDUP_MS", 1000)++ mw = LoggingMiddleware(_status_app(200))+ for repo in (b"model=a%2Fone", b"model=b%2Ftwo"):+ scope = _http_scope("/api/inference/audio/stt/status")+ scope["query_string"] = repo+ _run(mw(scope, _noop_receive, _drop))++ assert len(_paths_logged(logs)) == 2, _paths_logged(logs)+++def test_non_liveness_quiet_polls_keep_their_own_heartbeat(logs, monkeypatch):+ # Only the liveness group is shared. These report on different subsystems, so+ # collapsing them together would genuinely lose information.+ monkeypatch.setattr(hmod, "_ACCESS_LOG_DEDUP_MS", 0)+ monkeypatch.setattr(hmod, "_QUIET_POLL_DEDUP_MS", 1000)++ mw = LoggingMiddleware(_status_app(200))+ others = (+ "/api/train/runs",+ "/api/models/checkpoints",+ "/api/models/local",+ "/api/rag/knowledge-bases",+ )+ for _ in range(3):+ for path in others:+ _run(mw(_http_scope(path), _noop_receive, _drop))++ paths = _paths_logged(logs)+ for path in others:assert paths.count(path) == 1, f"{path} logged {paths.count(path)} times"+def test_a_failing_liveness_poll_always_logs(logs, monkeypatch):+ # Sharing a bucket must not hide a health check that starts failing: non-2xx+ # never dedups, so every failure logs even mid-burst.+ monkeypatch.setattr(hmod, "_ACCESS_LOG_DEDUP_MS", 0)+ monkeypatch.setattr(hmod, "_QUIET_POLL_DEDUP_MS", 1000)++ ok = LoggingMiddleware(_status_app(200))+ bad = LoggingMiddleware(_status_app(503))+ bad._last_log = ok._last_log # same middleware instance state+ for _ in range(3):+ _run(ok(_http_scope("/api/health"), _noop_receive, _drop))+ _run(bad(_http_scope("/api/inference/status"), _noop_receive, _drop))++ statuses = [e[2]["status_code"] for e in logs.events]+ assert statuses.count(503) == 3, statuses+ assert statuses.count(200) == 1, statuses+++def test_verbose_restores_every_liveness_line(logs, monkeypatch):+ monkeypatch.setattr(hmod, "_ACCESS_LOG_DEDUP_MS", 0)+ monkeypatch.setattr(hmod, "_QUIET_POLL_DEDUP_MS", 0)++ mw = LoggingMiddleware(_status_app(200))+ for _ in range(3):+ for path in ("/api/health", "/api/inference/status"):+ _run(mw(_http_scope(path), _noop_receive, _drop))++ assert len(_paths_logged(logs)) == 6, _paths_logged(logs)++def test_verbose_restores_the_dropped_success_polls(logs, monkeypatch):# --verbose zeroes both windows, so the 2xx suppressor must stand down too.monkeypatch.setattr(hmod, "_ACCESS_LOG_DEDUP_MS", 0)