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
Make the legacy-migration picker step deterministic, and unit-test the migration (#9603)
Daniel Han501afd26d8ed
diff --git a/studio/frontend/tests/legacy-load-settings-migration.test.ts b/studio/frontend/tests/legacy-load-settings-migration.test.tsnew file mode 100644index 00000000000..6de4cd753e0--- /dev/null+++ b/studio/frontend/tests/legacy-load-settings-migration.test.ts@@ -0,0 +1,170 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import { fileURLToPath, pathToFileURL } from "node:url";+import test from "node:test";++import { installLocalStorageFake, registerBundlerResolver } from "./helpers/kit.ts";++registerBundlerResolver();+const { store } = installLocalStorageFake();++/**+ * The one-time import of a pre-feature `unsloth_load_settings` store into the versioned+ * `unsloth_model_configs` map.+ *+ * Until now this had no test at all. Its only coverage was one step of+ * tests/studio/playwright_model_config.py, which needs a browser, a booted Studio and a+ * downloaded GGUF, and which races that Studio's own network traffic -- so a defect here+ * surfaced as an intermittent red on a job nobody could run locally, and the assertion it+ * failed ("not migrated into unsloth_model_configs") named the migration for damage the+ * migration had not done. These pin the contract directly, in milliseconds.+ *+ * Behaviour, not storage shape. Every assertion below goes through the module's own+ * public read path (`resolveInitialConfig`), so re-keying or re-versioning the records is+ * free; only a change to what a user's remembered settings DO can turn these red.+ */++const MODULE_PATH = fileURLToPath(+ new URL(+ "../src/features/model-picker/model-config/per-model-config.ts",+ import.meta.url,+ ),+);++const MODEL_ID = "unsloth/gemma-3-270m-it-GGUF";+const VARIANT = "UD-Q4_K_XL";+const LEGACY_KEY = `${MODEL_ID}::${VARIANT}`;+const CTX = 4096;++/**+ * A fresh copy of the module, because the migration is latched twice over: a persistent+ * `unsloth_model_configs_migrated` flag AND a module-level `legacyMigrationChecked`. A+ * second case sharing one instance would exercise the latch, not the migration.+ *+ * The query has to go on an absolute `file:` URL. tests/bundler-resolver.mjs round-trips a+ * relative specifier through `fileURLToPath`, which drops it, and every case would then+ * silently share the first instance.+ */+async function freshModule() {+ const url = pathToFileURL(MODULE_PATH);+ url.search = `?fresh=${Math.random()}`;+ return (await import(url.href)) as typeof import(+ "../src/features/model-picker/model-config/per-model-config.ts"+ );+}++function seedLegacy(entry: Record<string, unknown>, key = LEGACY_KEY): void {+ store.set("unsloth_load_settings", JSON.stringify({ [key]: entry }));+}++const FULL_LEGACY_ENTRY = {+ contextLength: CTX,+ kvCacheDtype: "q8_0",+ tensorParallel: true,+};++test("a legacy entry is remembered with the values it carried", async () => {+ store.clear();+ seedLegacy(FULL_LEGACY_ENTRY);+ const { resolveInitialConfig } = await freshModule();++ const { config, remembered } = resolveInitialConfig(MODEL_ID, VARIANT);++ // The context is the field that went missing in CI, but asserting only it would let a+ // migration that carried nothing else pass.+ assert.equal(remembered, true);+ assert.equal(config.customContextLength, CTX);+ assert.equal(config.kvCacheDtype, "q8_0");+ assert.equal(config.tensorParallel, true);+});++test("the model is remembered under the id and quant the picker asks by, whatever the legacy key spelled", async () => {+ store.clear();+ // The legacy key folds a repo id and a quant into one string on the last "::". Case is+ // the picker's to normalise, so asking in a different one must still find it.+ seedLegacy(FULL_LEGACY_ENTRY);+ const { resolveInitialConfig } = await freshModule();++ const asked = resolveInitialConfig(MODEL_ID.toLowerCase(), VARIANT.toLowerCase());++ assert.equal(asked.remembered, true);+ assert.equal(asked.config.customContextLength, CTX);+});++test("migrating once is enough: a later legacy store is not imported again", async () => {+ store.clear();+ seedLegacy(FULL_LEGACY_ENTRY);+ const first = await freshModule();+ assert.equal(first.resolveInitialConfig(MODEL_ID, VARIANT).config.customContextLength, CTX);++ // A fresh document (new module instance) with a DIFFERENT legacy store present. Re-running+ // the import on every reload is the regression that reverted the predecessor of #7207: a+ // model the user has since forgotten would come back from the legacy blob for ever.+ seedLegacy({ contextLength: CTX + 2048, tensorParallel: true }, "unsloth/other-model::Q4_K_M");+ const second = await freshModule();++ assert.equal(+ second.resolveInitialConfig("unsloth/other-model", "Q4_K_M").remembered,+ false,+ "a second document re-ran the one-time legacy import",+ );+ // and the first import is untouched.+ assert.equal(second.resolveInitialConfig(MODEL_ID, VARIANT).config.customContextLength, CTX);+});++test("settings saved in this build outrank a legacy blob for the same model", async () => {+ store.clear();+ // Saved FIRST, with no legacy store in sight, so this record exists before the import+ // has ever looked at the model. Ordering it the other way round -- seed, then save --+ // is the trap: the save itself triggers the import, so the precedence branch never+ // runs and the case passes no matter what that branch does. Verified by deleting the+ // `Object.hasOwn(map, key)` guard in mergeLegacyEntries, which this catches and the+ // seed-then-save ordering did not.+ const saver = await freshModule();+ saver.savePerModelConfig(MODEL_ID, VARIANT, {+ ...saver.DEFAULT_PER_MODEL_CONFIG,+ customContextLength: 16384,+ });++ // The flag postdates the versioned store, so an install genuinely reaches this shape:+ // records written by this build, a legacy blob still lying about from before the+ // upgrade, and no record of the import having run. Whatever wrote the record knew+ // about the versioned store, so it is strictly newer than a blob no build has written+ // since. Letting the legacy value win here would be the real data loss.+ store.delete("unsloth_model_configs_migrated");+ seedLegacy(FULL_LEGACY_ENTRY);++ const fresh = await freshModule();+ const { config } = fresh.resolveInitialConfig(MODEL_ID, VARIANT);++ assert.equal(config.customContextLength, 16384);+ // and the legacy blob does not get to half-apply itself over the saved record either.+ assert.equal(config.kvCacheDtype, null);+});++test("a legacy blob carrying nothing but defaults does not make a model look remembered", async () => {+ store.clear();+ // An all-defaults record would show "Remember for this model" ticked for a model the+ // user never configured, and pin it against later changes to the app defaults.+ seedLegacy({ tensorParallel: false });+ const { resolveInitialConfig } = await freshModule();++ assert.equal(resolveInitialConfig(MODEL_ID, VARIANT).remembered, false);+});++test("no legacy store at all leaves the model unremembered rather than throwing", async () => {+ store.clear();+ const { resolveInitialConfig } = await freshModule();++ assert.equal(resolveInitialConfig(MODEL_ID, VARIANT).remembered, false);+});++test("an unreadable legacy store is survived, not propagated", async () => {+ store.clear();+ store.set("unsloth_load_settings", "{not json");+ const { resolveInitialConfig } = await freshModule();++ assert.equal(resolveInitialConfig(MODEL_ID, VARIANT).remembered, false);+});diff --git a/tests/studio/playwright_model_config.py b/tests/studio/playwright_model_config.pyindex e220b28c883..3b3210aad71 100644--- a/tests/studio/playwright_model_config.py+++ b/tests/studio/playwright_model_config.py@@ -974,46 +974,252 @@ with sync_playwright() as p:# reload was the regression that reverted the predecessor PR.# ─────────────────────────────────────────────────────step("legacy unsloth_load_settings migrates once and stays idempotent")++ _seed_marks = [0]++ def seed_legacy_for_next_document(seed: dict, *, wipe_migrated: bool) -> None:+ """Put the legacy store in place for the NEXT document, before any app code runs.++ Not `evaluate` on the live page, which is what this used to do. Writing the+ seed into the document that is about to be discarded makes the assertion race+ that document's own in-flight work: `savePerModelConfig` in+ model-config-page.tsx runs from the `.then()` of a GET+ /api/settings/openai-auto-switch/overrides, so a response that arrives in the+ window between `removeItem('unsloth_model_configs')` and the navigation+ RE-CREATES the key from the server row. The reloaded page then finds that key+ already present, `mergeLegacyEntries` skips the legacy entry it was seeded to+ migrate (`Object.hasOwn(map, key)`), and `migrateLegacyLoadSettingsOnce`+ latches `unsloth_model_configs_migrated` anyway -- so the step reported the+ migration dropping a value when nothing had migrated at all.++ That window is roughly 20ms wide and only opens when the previous step's Load+ outruns its own wait, which is why this failed intermittently rather than+ every time: three reds in fourteen main runs, with the commit that introduced+ the write-back itself green.++ An init script runs at document start on the new page, after every write from+ the old document has gone with it, so the store the migration reads is the one+ this step asked for. The sessionStorage mark keeps it to a single document:+ init scripts cannot be removed, and re-seeding on the reload below would+ re-arm the very legacy entry the idempotency half needs to stay absent.+ """+ _seed_marks[0] += 1+ mark = f"__ui_modelcfg_seed_{_seed_marks[0]}"+ page.add_init_script(+ "(() => {\n"+ f" const MARK = {json.dumps(mark)};\n"+ " try {\n"+ " if (sessionStorage.getItem(MARK)) { return; }\n"+ " sessionStorage.setItem(MARK, '1');\n"+ f" localStorage.setItem('unsloth_load_settings', {json.dumps(json.dumps(seed))});\n"+ + (+ " localStorage.removeItem('unsloth_model_configs');\n"+ " localStorage.removeItem('unsloth_model_configs_migrated');\n"+ if wipe_migrated+ else ""+ )+ + " } catch (e) {}\n"+ "})();"+ )++ def clear_server_overrides_for_model() -> None:+ """Drop the server-side override rows for the model under test.++ This step clears localStorage and nothing else, which was fine while local+ storage was the only thing feeding the panel. It is not any more: the shared+ override row now outranks the local record, and opening the panel writes the+ row back down into `unsloth_model_configs`. Steps 2 to 4 above each mirror a+ save to the server and nothing removes it, so what this step actually measured+ was the migration racing that write-back -- and the migrated value lost+ whenever the row carried a context of its own.++ Seen on main as two different-looking failures from the same cause: an entry+ with nothing from the legacy seed in it (row written over a wiped map), and an+ entry carrying the seed's kvCacheDtype but step 3b's context (row written over+ the migrated map). Removing the row leaves the legacy import as the only thing+ that can put a value in this key, which is what the step is about.+ """+ # MODEL AND QUANT NORMALISED SEPARATELY, which is what `entries_for_model` already does+ # to these same two values. Folding the whole `<model>:<quant>` string as one identity+ # only works while the model half folds too: `normalizeModelIdentity` deliberately keeps+ # a plain POSIX path's case, so with a local-path GGUF_REPO the row+ # `/models/Foo.gguf:UD-Q4_K_XL` normalises to itself and never matched the lowercased+ # `...:ud-q4_k_xl` this was comparing against. The stale row then survived the cleanup and+ # the migration check went on to measure server precedence instead.+ want_model = _normalize_model_identity(GGUF_REPO)+ want_quant = GGUF_VARIANT.strip().lower()++ def _is_row_for_model(key: str) -> bool:+ if _normalize_model_identity(key) == want_model:+ return True+ model, sep, quant = key.rpartition(":")+ return bool(sep) and (+ _normalize_model_identity(model) == want_model+ and quant.strip().lower() == want_quant+ )++ def rows_for_model() -> list[str] | None:+ """The override rows for this model, or None if the inventory could not be READ.++ None is not the empty list. `evaluate_fetch` reports a timeout or an HTTP error by+ returning `status == 0` / a non-None `error` rather than raising, so a request that+ never landed used to come back as "there are no override rows" -- the cleanup did+ nothing, its own post-delete verification passed on the same silence, and stale rows+ went on to contaminate the migration check with no line of output saying so.+ """+ resp = evaluate_fetch(+ page,+ f"{BASE}/api/settings/openai-auto-switch/overrides",+ headers = {"Authorization": f"Bearer {token}"},+ )+ if not resp.get("status") or resp.get("error") is not None:+ return None+ body = resp.get("body")+ if not isinstance(body, dict) or not isinstance(body.get("overrides"), dict):+ return None+ return [k for k in body["overrides"] if _is_row_for_model(str(k))]++ def remove_rows(keys: list[str]) -> None:+ for key in keys:+ evaluate_fetch(+ page,+ f"{BASE}/api/settings/openai-auto-switch/overrides",+ method = "PUT",+ headers = {+ "Authorization": f"Bearer {token}",+ "Content-Type": "application/json",+ },+ body = {"model_id": key, "remove": True},+ )++ stale = rows_for_model()+ if stale is None:+ runtime_warn(+ "could not read the server override inventory, so the rows left by the earlier "+ "steps were not cleared; the migration check below may be measuring server "+ "precedence instead"+ )+ return+ remove_rows(stale)+ # AND IT HAS TO STAY REMOVED. `syncModelOverride` is fire-and-forget, so a mirror PUT+ # from steps 2 to 4 can still be in flight when this runs and recreate the row moments+ # after a single post-delete read found it gone -- which puts back exactly the+ # contamination this cleanup exists to remove. So absence is confirmed over a short+ # window rather than at one instant, and a row that comes back is removed again.+ # EVERY INTERVAL, not until the first empty one. Breaking on the first empty sample+ # confirms absence at one instant plus 250 ms, which is the same single-read weakness one+ # step further along: a queued PUT arriving in the third interval still recreates the row+ # before hydration reads it. The window is only a window if it is sampled to the end.+ left: list[str] | None = []+ for _ in range(4):+ page.wait_for_timeout(250)+ seen_now = rows_for_model()+ if seen_now is None:+ left = None+ break+ if seen_now:+ remove_rows(seen_now)+ left = seen_now+ if left is None:+ runtime_warn(+ "could not re-read the server override inventory after clearing it, so whether "+ "the rows stayed removed is unknown; the migration check below may be measuring "+ "server precedence instead"+ )+ elif left:+ # Not fatal on its own: say so rather than let the migration assertion below+ # report the leftover row as the migration losing a value.+ runtime_warn(+ f"server override rows for the model under test survived removal: {left}; "+ "the migration check below may be measuring server precedence instead"+ )+ elif stale:+ info(f"cleared {len(stale)} server override row(s) left by the earlier steps")++ def wait_for_migration_settled(timeout_ms: int = 15_000) -> str | None:+ """Block until the legacy import has actually run, and return its flag.++ The condition, not a sleep. `migrateLegacyLoadSettingsOnce` sets+ `unsloth_model_configs_migrated` as its last act on every path it takes --+ imported, nothing to import, legacy store unreadable -- so the flag appearing+ is exactly "the import has been and gone", which is what the assertions below+ need to be true before they read anything. A fixed wait either reads too early+ on a slow runner, which is how this step reported a value missing that was+ about to be written, or pads every green run with time it does not need.+ """+ deadline = time.monotonic() + timeout_ms / 1000+ flag = None+ while time.monotonic() < deadline:+ flag = robust_evaluate(+ page, "() => localStorage.getItem('unsloth_model_configs_migrated')"+ )+ if flag is not None:+ return flag+ page.wait_for_timeout(100)+ return flag+try:+ clear_server_overrides_for_model()legacy_key = f"{GGUF_REPO}::{GGUF_VARIANT}"legacy = {legacy_key: {"contextLength": DISTINCT_CTX,"kvCacheDtype": "q8_0","tensorParallel": True,+ # A fingerprint, and the reason it is this field: no other step here sets+ # Disable Vision, and toApiOverride only sends disable_vision when true,+ # so a record carrying it can only have come from this seed. The context+ # alone cannot tell "the import ran and lost a field" from "the import+ # never ran", and kvCacheDtype and tensorParallel cannot either -- step 4+ # writes both to the server, so a row mirrored back down reproduces them.+ "disableVision": True,}}- robust_evaluate(- page,- "(seed) => {"- " localStorage.setItem('unsloth_load_settings', JSON.stringify(seed));"- " localStorage.removeItem('unsloth_model_configs');"- " localStorage.removeItem('unsloth_model_configs_migrated');"- " return true;"- "}",- arg = legacy,- )+ seed_legacy_for_next_document(legacy, wipe_migrated = True)page.reload()composer = page.locator('textarea[aria-label="Message input"]')composer.wait_for(state = "visible", timeout = 60_000)# Opening the picker config forces the store to read (which migrates).popover = open_picker()open_config(popover, MODEL_HINT)- page.wait_for_timeout(800)+ flag_first = wait_for_migration_settled()cfg_first = read_configs()- migrated_ctx = any(- e.get("customContextLength") == DISTINCT_CTX for e in entries_for_model(cfg_first)- )- if migrated_ctx:+ model_entries = entries_for_model(cfg_first)+ migrated_ctx = any(e.get("customContextLength") == DISTINCT_CTX for e in model_entries)+ # Did the import run at all? Two very different failures were being reported as+ # one. "It ran and lost the context" is a bug in the migration; "nothing from+ # this seed is here" means the key was written by something else and the import+ # skipped it, which is what a racing write produces. Collapsing them into "not+ # migrated" sent the last investigation into normalizeV1, which was innocent.+ migrated_any = any(e.get("disableVision") is True for e in model_entries)+ # AND THE PASS REQUIRES THE FINGERPRINT TOO. The context alone does not say where it came+ # from: a server override row that survived the cleanup carries DISTINCT_CTX as well --+ # step 3b wrote it -- and hydration can put that into `model_entries` with the legacy+ # import never having applied the seed at all. Reporting OK on the context by itself is+ # the very confusion `disableVision` was added to end, left out of the branch that+ # decides whether this step passed.+ if migrated_ctx and migrated_any:info(f"OK migration: legacy context {DISTINCT_CTX} preserved after migrating")+ elif migrated_ctx:+ soft_fail(+ f"legacy context {DISTINCT_CTX} is present but NOTHING ELSE from the seed is: "+ f"disableVision is the fingerprint that only this seed sets, so the context was "+ f"put here by something other than the legacy import -- a surviving server "+ f"override row hydrating over the map is what produces this "+ f"(entries={json.dumps(model_entries)[:400]})"+ )+ elif migrated_any:+ soft_fail(+ f"legacy context {DISTINCT_CTX} was DROPPED by the migration: other fields "+ f"of the same legacy entry did land, so migrateLegacyLoadSettingsOnce ran "+ f"and lost the context (entries={json.dumps(model_entries)[:400]})"+ )else:soft_fail(- f"legacy context {DISTINCT_CTX} not migrated into unsloth_model_configs "- f"(got {json.dumps(cfg_first)[:400]})"+ f"legacy entry never migrated at all: nothing from the seed reached "+ f"unsloth_model_configs, so the key was already occupied when "+ f"migrateLegacyLoadSettingsOnce ran (cfg={json.dumps(cfg_first)[:400]})")- flag_first = robust_evaluate(- page, "() => localStorage.getItem('unsloth_model_configs_migrated')"- )if flag_first != "1":soft_fail(f"migration flag not set after migrating (got {flag_first!r})")shoot("09-after-migration")@@ -1024,19 +1230,23 @@ with sync_playwright() as p:# in, nothing duplicates, and the migrated value is untouched.if migrated_ctx:probe_key = "unsloth/__idem_probe__::Q4_K_M"- robust_evaluate(- page,- "(seed) => {"- " localStorage.setItem('unsloth_load_settings', JSON.stringify(seed));"- " return true;"- "}",- arg = {probe_key: {"contextLength": DISTINCT_CTX + 2048, "tensorParallel": True}},+ # Same document-start seeding as the first half, and for the same reason:+ # this one deliberately leaves `unsloth_model_configs` alone, so a write+ # racing the navigation would land in the very map the assertions below+ # compare key-for-key.+ seed_legacy_for_next_document(+ {probe_key: {"contextLength": DISTINCT_CTX + 2048, "tensorParallel": True}},+ wipe_migrated = False,)page.reload()composer.wait_for(state = "visible", timeout = 60_000)popover = open_picker()open_config(popover, MODEL_HINT)- page.wait_for_timeout(800)+ # The flag is already "1" here, so this waits on the store having been read+ # by the reloaded page rather than on the import: `readMap` is what would+ # re-run the import if the flag were being ignored, which is the regression+ # this half exists to catch.+ wait_for_migration_settled()cfg_second = read_configs()keys_first = set(cfg_first.keys())keys_second = set(cfg_second.keys())