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
Read the routing invariance counters as a steady state, not one request (#11389)
Daniel Han755212174028
diff --git a/studio/backend/tests/multi_account/test_routing_invariance.py b/studio/backend/tests/multi_account/test_routing_invariance.pyindex 75023f2809d..085aa28d7ba 100644--- a/studio/backend/tests/multi_account/test_routing_invariance.py+++ b/studio/backend/tests/multi_account/test_routing_invariance.py@@ -1,7 +1,26 @@# SPDX-License-Identifier: AGPL-3.0-only# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0-"""Per-request I/O never depends on how many accounts exist, and the owner's routing is fixed."""+"""Per-request I/O never depends on how many accounts exist, and the owner's routing is fixed.++The counters below are read from a WARM request, because the interesting claim is about+steady state: a first request legitimately builds caches and opens the databases it will+reuse, and counting that would only ever measure start-up. Two unmeasured requests warm+it, and the third is the one read.++Two turned out not to be enough. Run 35530114495 read /api/chat/threads at 3 mkdir, 3+connections and 86 statements where the same request in the same test had just measured+2, 2 and 8, on a commit whose only shared-path change was error-message wording, and the+identical job passed on re-run. Warm-up cannot bound lazy work that is triggered by a+clock rather than by a request count: anything with a TTL can expire between the warm-up+and the measurement and be rebuilt inside it.++What that noise can do is add work, never remove it. So the measurement is repeated and+each counter is taken at its MINIMUM, which is the steady-state cost; a rebuild that+fires in one repeat is not in the others. A cost that is genuinely higher with more+accounts is higher in every repeat and still fails, which is what _steady_cost_keeps_a_+persistent_increase checks directly rather than by assertion.+"""import osimport secrets@@ -26,6 +45,26 @@ def _create(names):policy.invalidate_account_cache()+# Three readings, not two: two cannot tell which of a disagreeing pair is the steady state.+_REPEATS = 3+++def _steady(readings: list) -> dict:+ """The per-counter minimum across readings of the same request.++ Lazily rebuilt state adds work to whichever reading it lands in and takes none away,+ so the smallest reading of each counter is the cost with nothing rebuilding. A cost+ that really did grow grew in all of them, and the minimum grows with it.+ """+ keys = set().union(*readings) if readings else set()+ steady = {key: min(reading.get(key, 0) for reading in readings) for key in keys}+ # A counter only present in the noisy reading has a minimum of zero, and a zero is the+ # same statement as the key being absent: Counter never records one. Keeping it would+ # turn the noise back into a difference under the `==` below, which is the whole thing+ # this is trying to stop.+ return {key: value for key, value in steady.items() if value}++def _vector(client, headers, path) -> dict:"""sqlite connections, statements, mkdirs and auth.db count queries for one warm request."""counters = Counter()@@ -49,13 +88,17 @@ def _vector(client, headers, path) -> dict:for _ in range(2):assert client.get(path, headers = headers).status_code == 200- with (- patch.object(sqlite3, "connect", open_connection),- patch.object(os, "mkdir", create_directory),- patch.object(storage, "account_counts", counted),- ):- assert client.get(path, headers = headers).status_code == 200- return dict(counters)+ readings = []+ for _ in range(_REPEATS):+ counters.clear()+ with (+ patch.object(sqlite3, "connect", open_connection),+ patch.object(os, "mkdir", create_directory),+ patch.object(storage, "account_counts", counted),+ ):+ assert client.get(path, headers = headers).status_code == 200+ readings.append(dict(counters))+ return _steady(readings)def _vectors(client, username):@@ -102,3 +145,38 @@ def test_owner_routing_facts_and_shared_queue(isolated_auth, account_client):assert run_as(OWNER, get_llama_admission_queue, key) is run_as(managed, get_llama_admission_queue, key)+++def test_steady_cost_keeps_a_persistent_increase_and_drops_a_one_off():+ """The minimum above must not be able to hide a cost that is really there.++ Read directly rather than through a request, because the two cases it has to tell+ apart are exactly the ones a live request cannot be made to produce on demand.+ """+ baseline = [{"connections": 2, "statements": 8}] * _REPEATS++ one_off = [{"connections": 2, "statements": 8}] * _REPEATS+ one_off[1] = {"connections": 3, "statements": 86}+ assert _steady(one_off) == _steady(baseline), (+ "a rebuild landing in one reading is not the steady-state cost, and taking the "+ "minimum is what makes that true"+ )++ persistent = [{"connections": 3, "statements": 9}] * _REPEATS+ assert _steady(persistent) != _steady(baseline), (+ "a cost that is higher on every reading is a real increase; if the minimum "+ "swallowed it this guard would pass a per-account regression"+ )++ # A counter that appears in only one reading is absent from the others, so its minimum+ # is zero, and a zero has to be dropped rather than reported. Counter never records a+ # zero, so a reading that never saw the mkdir has no `mkdir` key at all, and keeping+ # `{"mkdir": 0}` would fail the `==` against it just as surely as `{"mkdir": 1}` would.+ appears_once = [{"connections": 2}, {"connections": 2, "mkdir": 1}, {"connections": 2}]+ never_appears = [{"connections": 2}] * _REPEATS+ assert _steady(appears_once) == {"connections": 2}+ assert _steady(appears_once) == _steady(never_appears), (+ "a one-off counter has to leave the steady vector, not sit in it at zero: the "+ "comparisons above are exact, and a key the other side has never heard of "+ "differs whatever its value"+ )