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: keep managed accounts off owner files and device nodes (#12661)
Daniel Han250abc723118
diff --git a/studio/backend/core/inference/llama_server_args.py b/studio/backend/core/inference/llama_server_args.pyindex 87e8acb7c9d..b8433a6c95f 100644--- a/studio/backend/core/inference/llama_server_args.py+++ b/studio/backend/core/inference/llama_server_args.py@@ -152,6 +152,54 @@ _DENYLIST_GROUPS: tuple[frozenset[str], ...] = (_DENYLIST: frozenset[str] = frozenset().union(*_DENYLIST_GROUPS)+# Pass-through flags whose value is a path the child opens (or, for --log-prompts-dir, writes under). llama-server runs+# as the installation's OS user, and account model access is checked for the model being loaded, not for these, so in+# multi-account mode only the owner may name one.+OWNER_ONLY_PATH_FLAGS: frozenset[str] = frozenset(+ {+ "-mm",+ "--mmproj",+ "--spec-draft-model",+ "-md",+ "--model-draft",+ "-mv",+ "--model-vocoder",+ "--lora",+ "--lora-scaled",+ "--control-vector",+ "--control-vector-scaled",+ "--chat-template-file",+ "--grammar-file",+ "-jf",+ "--json-schema-file",+ "-lcs",+ "--lookup-cache-static",+ "-lcd",+ "--lookup-cache-dynamic",+ "--log-prompts-dir",+ # llama-server runs <dir>/ffmpeg to decode a video.+ "--video-ffmpeg-dir",+ }+)+++def owner_only_path_args(args: Optional[Iterable[str]]) -> list[tuple[str, str]]:+ """``(flag, value)`` for each OWNER_ONLY_PATH_FLAGS occurrence in ``args``, in order."""+ tokens = [str(a) for a in args or ()]+ found: list[tuple[str, str]] = []+ for i, raw in enumerate(tokens):+ flag = _flag_name(raw)+ if flag in OWNER_ONLY_PATH_FLAGS:+ _, eq, inline = raw.partition("=")+ found.append((flag, inline if eq else (tokens[i + 1] if i + 1 < len(tokens) else "")))+ return found+++def owner_only_path_flags(args: Optional[Iterable[str]]) -> list[str]:+ """The OWNER_ONLY_PATH_FLAGS present in ``args``, in first-seen order."""+ return list(dict.fromkeys(flag for flag, _ in owner_only_path_args(args)))++# Flags that take TWO values rather than one. Scanned out of `llama-server --help`: every other option is `--flag# VALUE` or a switch, and this list exists so the positional check below does not refuse a legitimate second value._TWO_VALUE_FLAGS: frozenset[str] = frozenset({"--control-vector-layer-range"})diff --git a/studio/backend/core/inference/tool_confinement.py b/studio/backend/core/inference/tool_confinement.pyindex 19cc94f9a25..a9cfba9fb39 100644--- a/studio/backend/core/inference/tool_confinement.py+++ b/studio/backend/core/inference/tool_confinement.py@@ -30,6 +30,8 @@ _FS_EXECUTE = 1 << 0_FS_WRITE_FILE = 1 << 1_FS_READ_FILE = 1 << 2_FS_READ_DIR = 1 << 3+_FS_MAKE_CHAR = 1 << 6+_FS_MAKE_BLOCK = 1 << 11_FS_MAKE_SYM = 1 << 12_FS_REFER = 1 << 13 # ABI 2_FS_TRUNCATE = 1 << 14 # ABI 3@@ -310,6 +312,13 @@ def _handled_mask(abi: int) -> int:return mask+def _writable_access(handled: int) -> int:+ """Rights on a writable root. No symlink creation: the server follows links, so a tool must not+ plant an escaping one. No device nodes or device ioctls either: a root-run Studio would otherwise+ let a tool mknod a disk in its workspace; real devices come only from the explicit device rules."""+ return handled & ~(_FS_MAKE_SYM | _FS_MAKE_CHAR | _FS_MAKE_BLOCK | _FS_IOCTL_DEV)++def _device_nodes() -> list[str]:import globreturn _existing((*_DEVICE_NODES, *_ACCELERATOR_NODES, *sorted(glob.glob("/dev/nvidia[0-9]*"))))@@ -336,8 +345,7 @@ def _landlock_rules(abi: int, sandbox_site_dir: str) -> list[tuple[str, int]]:rules.append((path, read))for path in _device_nodes():rules.append((path, device | (_FS_READ_DIR if os.path.isdir(path) else 0)))- # No symlink creation: the server follows links, so a tool must not plant an escaping one.- writable = handled & ~_FS_MAKE_SYM+ writable = _writable_access(handled)for path in writable_roots:rules.append((path, writable))return rulesdiff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.pyindex 24a7f4ba47d..c1a8b618c19 100644--- a/studio/backend/models/inference.py+++ b/studio/backend/models/inference.py@@ -67,6 +67,9 @@ class LoadRequest(LlamaCppConfigFields):# `()` is both the default and auto-switch's deliberate "do not widen", so only this# marker separates unset from explicitly empty._gguf_companion_roots_set: bool = PrivateAttr(default = False)+ # Auto-switch only: the alias its owner-override lookup used, so the managed path-flag+ # check reads the same override row.+ _override_alias_id: Optional[str] = PrivateAttr(default = None)load_request_id: Optional[str] = Field(None,min_length = 1,diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 2801d159036..69267d9769f 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -7886,17 +7886,107 @@ def _external_transcript_preview(response: Response) -> str:return ""-def _refuse_managed_custom_projector(extra_args: Optional[list[str]]) -> None:- """A pass-through projector path skips account model access, so only the owner may name one."""- from core.inference.llama_cpp import _extra_args_device- if (- account_access.managed_account()- and _extra_args_device(extra_args, {"--mmproj", "-mm"}) is not None- ):+def _owner_chosen_launch(+ identifier,+ config_identifier = None,+ variant = None,+):+ """Path args and custom configs the owner already chose for this model: its saved override+ (written only through owner routes) and the resident same-model load (which passed this check+ or was the owner's). Replaying these is not a new path, so auto-switch loads and the UI resending+ an inherited or echoed setting keep working."""+ from core.inference.llama_custom_config import parse_config_source+ from core.inference.llama_server_args import owner_only_path_args+ from utils.openai_auto_switch_settings import resolve_override_for_load++ sources = []+ if identifier:+ _, override = resolve_override_for_load(identifier, config_identifier, variant)+ sources.append((override.get("llama_extra_args"), override.get("llama_cpp_config")))+ intent = getattr(get_llama_cpp_backend(), "last_load_intent", None)+ if (+ intent is not None+ # A snapshot path and its repo id are one model; other paths compare as before.+ and _same_loaded_identifier(+ _snapshot_repo_or_self(+ getattr(intent, "model_identifier", None), require_cache = True+ ),+ _snapshot_repo_or_self(identifier, require_cache = True),+ )+ # No variant named: the load resolves the same quant the resident one did.+ and (+ variant is None+ or (getattr(intent, "hf_variant", None) or "").casefold() == variant.casefold()+ )+ ):+ sources.append(+ (getattr(intent, "extra_args", None), getattr(intent, "llama_cpp_config", None))+ )+ pairs: set[tuple[str, str]] = set()+ configs = []+ for args, source in sources:+ pairs.update(owner_only_path_args(args))+ if source is not None:+ configs.append(parse_config_source(source))+ return pairs, configs+++def _snapshot_repo_or_self(model_id, require_cache = False):+ from core.inference.model_ids import hf_cache_repo_id++ repo = hf_cache_repo_id(model_id) if model_id else None+ # A caller's look-alike models--org--name/snapshots path outside the HF cache is not that repo.+ if repo and (not require_cache or _inside_hf_cache(model_id)):+ return repo+ return model_id+++def _inside_hf_cache(path) -> bool:+ from utils.hf_cache_settings import known_hf_hub_caches++ target = os.path.normcase(os.path.abspath(str(path)))+ for root in known_hf_hub_caches():+ root = os.path.normcase(os.path.abspath(str(root)))+ try:+ if os.path.commonpath([root, target]) == root:+ return True+ except ValueError:+ continue+ return False+++def _refuse_managed_custom_projector(+ extra_args: Optional[list[str]],+ identifier: Optional[str] = None,+ config_identifier: Optional[str] = None,+ variant: Optional[str] = None,+ sent_config = None,+) -> None:+ """A pass-through path (projector, drafter, adapter, template, grammar...) skips account model+ access, so only the owner may name one. Values the owner already chose for this model pass."""+ from core.inference.llama_server_args import owner_only_path_args++ if not account_access.managed_account():+ return+ found = owner_only_path_args(extra_args)+ if not found:+ return+ owner_pairs, owner_configs = _owner_chosen_launch(identifier, config_identifier, variant)+ if sent_config is not None:+ from core.inference.llama_custom_config import parse_config_source+ if parse_config_source(sent_config) in owner_configs:+ return+ flags = list(dict.fromkeys(flag for flag, value in found if (flag, value) not in owner_pairs))+ if {"--mmproj", "-mm"} & set(flags):raise HTTPException(status_code = 403,detail = "A custom --mmproj path is available to this installation's owner only.",)+ if flags:+ raise HTTPException(+ status_code = 403,+ detail = f"File path options ({', '.join(flags)}) are available to this installation's owner only.",+ )def _validate_native_gguf_companion(@@ -11291,6 +11381,7 @@ async def _maybe_auto_switch_model(load_request = LoadRequest(**load_kwargs)load_request._gguf_companion_roots = gguf_companion_rootsload_request._gguf_companion_roots_set = True+ load_request._override_alias_id = override_idawait _load_model_impl(load_request,fastapi_request,@@ -11317,6 +11408,7 @@ async def _maybe_auto_switch_model(load_request = LoadRequest(**load_kwargs)load_request._gguf_companion_roots = gguf_companion_rootsload_request._gguf_companion_roots_set = True+ load_request._override_alias_id = override_idawait _load_model_impl(load_request,fastapi_request,@@ -16273,8 +16365,14 @@ def _resolve_llama_cpp_config(return request.model_copy(update = {"llama_cpp_config": source.to_wire()})-async def _preflight_custom_llama_config(request, config):- """Compile a custom config before any backend or GPU owner can be evicted."""+async def _preflight_custom_llama_config(+ request,+ config,+ *,+ caller_sent_custom = False,+):+ """Compile a custom config before any backend or GPU owner can be evicted. A config the caller+ sent itself (not the owner's saved override) is held to the same path rules as pass-through args."""if not _custom_llama_config(request):return Noneif not config.is_gguf or _classify_diffusion_gguf(config) is True:@@ -16286,9 +16384,18 @@ async def _preflight_custom_llama_config(request, config):model_identifier = config.identifier, llama_cpp_config = request.llama_cpp_config)try:- return await asyncio.to_thread(get_llama_cpp_backend().prepare_custom_config, intent)+ compiled = await asyncio.to_thread(get_llama_cpp_backend().prepare_custom_config, intent)except ValueError as exc:raise HTTPException(status_code = 400, detail = redact_native_paths(str(exc))) from exc+ if caller_sent_custom:+ _refuse_managed_custom_projector(+ list(compiled.argv),+ request.model_path,+ getattr(request, "_override_alias_id", None) or config.identifier,+ getattr(request, "gguf_variant", None) or getattr(config, "gguf_variant", None),+ sent_config = request.llama_cpp_config,+ )+ return compileddef _resolve_inherited_extra_args(@@ -16407,6 +16514,13 @@ def _resolve_inherited_extra_args("load (same model, shadow-stripped): %s",extra_llama_args,)+ # Inherited path options get the same owner-only check as a sent list.+ _refuse_managed_custom_projector(+ extra_llama_args,+ getattr(request, "model_path", None),+ getattr(request, "_override_alias_id", None),+ getattr(config, "gguf_variant", None),+ )return extra_llama_args@@ -17866,7 +17980,9 @@ async def _load_model_impl(extra_llama_args: Optional[list[str]] = (None if request.llama_extra_args is None else extra_llama_args)- _refuse_managed_custom_projector(extra_llama_args)+ _refuse_managed_custom_projector(+ extra_llama_args, request.model_path, request._override_alias_id, request.gguf_variant+ )_reasoning_updates = {}_reasoning_budget_override = parse_reasoning_budget_override(extra_llama_args)@@ -18208,7 +18324,12 @@ async def _load_model_impl(public_model_identifier,)extra_llama_args = _validated_extra_args(request)- custom_compiled = await _preflight_custom_llama_config(request, config)+ custom_compiled = await _preflight_custom_llama_config(+ request,+ config,+ caller_sent_custom = isinstance(requested_llama_cpp_config, dict)+ and requested_llama_cpp_config.get("mode") == "custom",+ )if custom_compiled is not None:_n_parallel = custom_compiled.n_parallel or _n_paralleleffective_chat_template_override = None@@ -19367,10 +19488,13 @@ async def validate_model(_reject_unsupported_managed_kind(request, config)await _managed_engine_options(request, config, request.hf_token)+ caller_sent_custom = _custom_llama_config(request)request = _resolve_llama_cpp_config(request, config, _public_model_identifier(request.model_path, model_identifier))- custom_compiled = await _preflight_custom_llama_config(request, config)+ custom_compiled = await _preflight_custom_llama_config(+ request, config, caller_sent_custom = caller_sent_custom+ )# The caller's own list when it sent one, or the resolver hands back this# fourth argument unchanged and a --ctx-size the load is about to use would@@ -19396,7 +19520,12 @@ async def validate_model(except ValueError as exc:raise HTTPException(status_code = 400, detail = str(exc)) from excif getattr(request, "llama_extra_args", None) is not None:- _refuse_managed_custom_projector(effective_extra_args)+ _refuse_managed_custom_projector(+ effective_extra_args,+ _public_model_identifier(request.model_path, model_identifier),+ config.identifier,+ getattr(request, "gguf_variant", None) or getattr(config, "gguf_variant", None),+ )# Manual mode owns the offload flags, and /load translates an explicit -ngl# into the first-class field before it strips them. Doing that there and not@@ -20381,6 +20510,14 @@ async def estimate_memory(request = _resolve_llama_cpp_config(request)if _custom_llama_config(request):return EstimateMemoryResponse(available = False, reason = "unsizable")+ # Sizing reads the files those flags name, so a path the load would refuse+ # is not sized either (the panel shows "unsizable", not an error).+ try:+ _refuse_managed_custom_projector(+ request.llama_extra_args, request.model_path, None, request.gguf_variant+ )+ except HTTPException:+ return EstimateMemoryResponse(available = False, reason = "unsizable")if is_ollama_manifest_ref(request.model_path):# Resolving one writes a .gguf link to disk; that belongs to the load path.return EstimateMemoryResponse(available = False, reason = "unsupported_source")diff --git a/studio/backend/tests/test_account_tool_confinement.py b/studio/backend/tests/test_account_tool_confinement.pyindex 485ea4becdd..121681234de 100644--- a/studio/backend/tests/test_account_tool_confinement.py+++ b/studio/backend/tests/test_account_tool_confinement.py@@ -297,30 +297,41 @@ def test_landlock_rules_cover_own_roots_only(tmp_path):run_as(ALICE, tools._get_workdir, "chat")rules = run_as(ALICE, tool_confinement._landlock_rules, 3, tools._SANDBOX_SITE_DIR)handled = tool_confinement._handled_mask(3)- writable = [p for p, access in rules if access == handled & ~tool_confinement._FS_MAKE_SYM]+ writable_access = tool_confinement._writable_access(handled)+ writable = [p for p, access in rules if access == writable_access]alice_root = str((tmp_path / "studio" / "accounts" / "alice-id").resolve())assert alice_root not in writableassert f"{alice_root}/sandbox" in writableassert str((tmp_path / "projects" / "Accounts" / "alice-id" / "Projects").resolve()) in writable- assert alice_root in [- p for p, access in rules if access != handled & ~tool_confinement._FS_MAKE_SYM- ]+ assert alice_root in [p for p, access in rules if access != writable_access]assert str((tmp_path / "studio").resolve()) not in [p for p, _ in rules]assert all(not p.startswith(str((tmp_path / "studio").resolve()) + os.sep) or p.startswith(alice_root)for p, _ in rules)- read_only = [- p- for p, access in rules- if access not in (handled, handled & ~tool_confinement._FS_MAKE_SYM)- ]+ read_only = [p for p, access in rules if access not in (handled, writable_access)]assert any(p.startswith(os.path.realpath(sys.prefix)) or os.path.realpath(sys.prefix).startswith(p)for p in read_only)[email protected]("abi", [1, 3, 5, 6])+def test_writable_roots_cannot_make_devices_or_links(abi):+ handled = tool_confinement._handled_mask(abi)+ writable = tool_confinement._writable_access(handled)+ for right in (+ tool_confinement._FS_MAKE_SYM,+ tool_confinement._FS_MAKE_CHAR,+ tool_confinement._FS_MAKE_BLOCK,+ tool_confinement._FS_IOCTL_DEV,+ ):+ assert not writable & right+ # Ordinary file work stays granted, and nothing outside the handled set leaks in.+ assert writable & tool_confinement._FS_WRITE_FILE and writable & tool_confinement._FS_READ_DIR+ assert writable & ~handled == 0++@pytest.mark.skipif(not LANDLOCK, reason = "Landlock not available on this kernel")def test_managed_child_cannot_plant_a_link_in_its_own_tree(tmp_path):"""The server follows links for the owner, so a managed child gets no way to make one."""@@ -337,6 +348,26 @@ def test_managed_child_cannot_plant_a_link_in_its_own_tree(tmp_path):assert "made" in out[email protected](not LANDLOCK, reason = "Landlock not available on this kernel")+def test_managed_child_cannot_make_a_device_node_in_its_own_tree(tmp_path):+ """Landlock refuses the mknod (EACCES) before the capability check (EPERM) is reached, so a+ root-run Studio cannot hand a tool a raw disk through its own workspace."""+ _seed(tmp_path)+ out = run_as(+ BOB,+ tools._python_exec,+ "import errno, os, stat\n"+ "try:\n"+ " os.mknod('dev0', 0o600 | stat.S_IFCHR, os.makedev(1, 3)); print('MADE')\n"+ "except OSError as e:\n"+ " print('mknod', errno.errorcode[e.errno])\n"+ "open('plain.txt', 'w').write('ok'); print('wrote', open('plain.txt').read())\n",+ session_id = "chat",+ )+ assert "MADE" not in out and "mknod EACCES" in out, out+ assert "wrote ok" in out, out++@pytest.mark.skipif(not LANDLOCK, reason = "Landlock not available on this kernel")def test_managed_child_cannot_signal_another_accounts_process(tmp_path):"""Two accounts' tools run as one Unix user; signals stay inside the child's own domain."""diff --git a/studio/backend/tests/test_llama_custom_routes.py b/studio/backend/tests/test_llama_custom_routes.pyindex 203db0b2e4a..fe5bd5b83d4 100644--- a/studio/backend/tests/test_llama_custom_routes.py+++ b/studio/backend/tests/test_llama_custom_routes.py@@ -162,6 +162,36 @@ async def test_preflight_refuses_a_bad_config_before_any_eviction(monkeypatch):assert calls == [CUSTOM["ini"]][email protected]("managed", [False, True])[email protected]("caller_sent_custom", [False, True])+async def test_preflight_holds_a_managed_callers_own_config_to_owner_only_paths(+ monkeypatch, managed, caller_sent_custom+):+ compiled = SimpleNamespace(argv = ("--chat-template-file", "/home/owner/secret.jinja"))+ monkeypatch.setattr(+ routes,+ "get_llama_cpp_backend",+ lambda: SimpleNamespace(prepare_custom_config = lambda intent: compiled),+ )+ monkeypatch.setattr(routes, "_classify_diffusion_gguf", lambda config: False)+ monkeypatch.setattr(routes.account_access, "managed_account", lambda: managed)+ request = LoadRequest(model_path = "model.gguf", llama_cpp_config = CUSTOM)+ config = SimpleNamespace(is_gguf = True, identifier = "model.gguf")+ if managed and caller_sent_custom:+ with pytest.raises(HTTPException) as caught:+ await routes._preflight_custom_llama_config(+ request, config, caller_sent_custom = caller_sent_custom+ )+ assert caught.value.status_code == 403+ return+ # The owner, or a managed load that inherits the owner's saved override, is unchanged.+ got = await routes._preflight_custom_llama_config(+ request, config, caller_sent_custom = caller_sent_custom+ )+ assert got is compiled++@pytest.mark.asyncioasync def test_preflight_refuses_non_gguf():request = LoadRequest(model_path = "org/model", llama_cpp_config = CUSTOM)diff --git a/studio/backend/tests/test_llama_server_args.py b/studio/backend/tests/test_llama_server_args.pyindex ee75f428dfb..5855f1b8b75 100644--- a/studio/backend/tests/test_llama_server_args.py+++ b/studio/backend/tests/test_llama_server_args.py@@ -1681,3 +1681,25 @@ def test_a_pageable_launch_keeps_every_token_including_its_lock():def test_custom_projector_is_allowed(flag):args = [flag, "/models/custom projector.gguf"]assert validate_extra_args(args) == args+++def test_owner_only_path_flags_names_each_file_option_once():+ args = [+ "--ctx-size",+ "4096",+ "--lora",+ "/a.gguf",+ "--lora",+ "/b.gguf",+ "--chat_template_file",+ "/t.jinja",+ "-jf",+ "/s.json",+ "--temp",+ "0.7",+ ]+ assert _lsa.owner_only_path_flags(args) == ["--lora", "--chat-template-file", "-jf"]+ assert _lsa.owner_only_path_flags(["--ctx-size", "4096", "-ngl", "99"]) == []+ assert _lsa.owner_only_path_flags(None) == []+ # Owners keep every one of them: the validator itself is unchanged.+ assert validate_extra_args(args) == argsdiff --git a/studio/backend/tests/test_memory_estimate.py b/studio/backend/tests/test_memory_estimate.pyindex 4878e8be834..bbc47b04986 100644--- a/studio/backend/tests/test_memory_estimate.py+++ b/studio/backend/tests/test_memory_estimate.py@@ -922,6 +922,27 @@ class TestEstimateMemoryRoute:assert resp.available is Falseassert resp.reason == "unsupported_source"+ def test_managed_caller_cannot_size_an_owner_only_path(self, monkeypatch):+ # Sizing reads the file a draft/projector/adapter flag names, so a path the+ # load would refuse is answered "unsizable" before anything is opened.+ from utils import openai_auto_switch_settings as settings++ def boom(*a, **kw):+ raise AssertionError("estimate-memory must not size a refused path")++ monkeypatch.setattr(ri.account_access, "managed_account", lambda: True)+ monkeypatch.setattr(ri.account_access, "require_model_access", lambda *a, **kw: None)+ monkeypatch.setattr(settings, "get_model_override", lambda key: {})+ monkeypatch.setattr(+ ri, "get_llama_cpp_backend", lambda: SimpleNamespace(last_load_intent = None)+ )+ monkeypatch.setattr(ri, "_cached_estimate_config", boom)+ resp = _estimate(+ model_path = "/models/m.gguf", llama_extra_args = ["-md", "/home/owner/private.gguf"]+ )+ assert resp.available is False+ assert resp.reason == "unsizable"+def test_the_estimate_asks_the_question_the_worker_asks_of_the_stack(self, monkeypatch):import utils.mlx_repair as repairmonkeypatch.setattr(repair, "is_apple_silicon", lambda: True)diff --git a/studio/backend/tests/test_mmproj_placement_policy.py b/studio/backend/tests/test_mmproj_placement_policy.pyindex 4326a293fb9..07d89e2b48c 100644--- a/studio/backend/tests/test_mmproj_placement_policy.py+++ b/studio/backend/tests/test_mmproj_placement_policy.py@@ -1833,3 +1833,224 @@ def test_only_the_owner_may_name_a_custom_projector(monkeypatch, flag, managed):with pytest.raises(HTTPException) as err:routes._refuse_managed_custom_projector([flag, "/models/p.gguf"])assert err.value.status_code == 403+++ "args",+ [+ ["--chat-template-file", "/home/owner/.ssh/id_ed25519"],+ ["--grammar-file", "/etc/shadow"],+ ["-jf", "/home/owner/secret.json"],+ ["--lora", "/home/owner/adapter.gguf"],+ ["--lora-scaled", "/home/owner/adapter.gguf:0.5"],+ ["--control-vector", "/home/owner/cv.gguf"],+ ["-md", "/home/owner/draft.gguf"],+ ["--spec-draft-model", "/home/owner/draft.gguf"],+ ["-lcs", "/home/owner/cache.bin"],+ ["--log-prompts-dir", "/home/owner/.config"],+ ["--video-ffmpeg-dir", "/srv/studio/accounts/m/sandbox"],+ ],+)[email protected]("managed", [False, True])+def test_only_the_owner_may_name_a_file_path_option(monkeypatch, args, managed):+ import routes.inference as routes+ from fastapi import HTTPException++ monkeypatch.setattr(routes.account_access, "managed_account", lambda: managed)+ routes._refuse_managed_custom_projector(["--ctx-size", "4096", "--temp", "0.7"])+ routes._refuse_managed_custom_projector(None)+ if not managed:+ routes._refuse_managed_custom_projector(["--ctx-size", "4096", *args])+ return+ with pytest.raises(HTTPException) as err:+ routes._refuse_managed_custom_projector(["--ctx-size", "4096", *args])+ assert err.value.status_code == 403 and args[0] in err.value.detail+ assert args[1] not in err.value.detail+++_OWNER_PATHS = ["--chat-template-file", "/owner/t.jinja", "-md", "/owner/draft.gguf"]+++def _managed_with_owner(+ monkeypatch,+ override = None,+ intent = None,+):+ import routes.inference as routes+ from types import SimpleNamespace+ from utils import openai_auto_switch_settings as settings++ monkeypatch.setattr(routes.account_access, "managed_account", lambda: True)+ monkeypatch.setattr(+ settings, "get_model_override", lambda key: dict(override.get(key, {})) if override else {}+ )+ monkeypatch.setattr(+ routes, "get_llama_cpp_backend", lambda: SimpleNamespace(last_load_intent = intent)+ )+ return routes+++def test_managed_caller_may_replay_the_owners_saved_paths(monkeypatch):+ # Auto-switch builds the load from the owner's saved override, keyed here by its alias.+ from fastapi import HTTPException++ routes = _managed_with_owner(monkeypatch, {"org/alias": {"llama_extra_args": _OWNER_PATHS}})+ routes._refuse_managed_custom_projector(+ ["--ctx-size", "4096", *_OWNER_PATHS], "m.gguf", "org/alias"+ )+ routes._refuse_managed_custom_projector(+ ["--chat-template-file=/owner/t.jinja"], "m.gguf", "org/alias"+ )+ with pytest.raises(HTTPException) as err:+ routes._refuse_managed_custom_projector(+ ["--chat-template-file", "/home/owner/.ssh/id"], "m.gguf", "org/alias"+ )+ assert err.value.status_code == 403+ with pytest.raises(HTTPException):+ routes._refuse_managed_custom_projector(_OWNER_PATHS, "other.gguf")+++def test_managed_caller_may_resend_the_resident_same_model_paths(monkeypatch):+ from fastapi import HTTPException+ from types import SimpleNamespace++ import utils.hf_cache_settings as cache_settings++ monkeypatch.setattr(cache_settings, "known_hf_hub_caches", lambda: [Path("/hf/hub")])++ intent = SimpleNamespace(+ model_identifier = "m.gguf",+ hf_variant = None,+ extra_args = ("--lora", "/owner/a.gguf"),+ llama_cpp_config = None,+ )+ routes = _managed_with_owner(monkeypatch, intent = intent)+ routes._refuse_managed_custom_projector(["--lora", "/owner/a.gguf"], "m.gguf")+ # Resident recorded as an HF cache snapshot path; the resend names the repo id.+ snapshot = SimpleNamespace(+ model_identifier = "/hf/hub/models--unsloth--B-GGUF/snapshots/abc/B-Q4_K_M.gguf",+ hf_variant = None,+ extra_args = ("--lora", "/owner/a.gguf"),+ llama_cpp_config = None,+ )+ monkeypatch.setattr(+ routes, "get_llama_cpp_backend", lambda: SimpleNamespace(last_load_intent = snapshot)+ )+ routes._refuse_managed_custom_projector(["--lora", "/owner/a.gguf"], "unsloth/B-GGUF")+ monkeypatch.setattr(+ routes, "get_llama_cpp_backend", lambda: SimpleNamespace(last_load_intent = intent)+ )+ with pytest.raises(HTTPException):+ routes._refuse_managed_custom_projector(["--lora", "/owner/a.gguf"], "other.gguf")+ with pytest.raises(HTTPException):+ routes._refuse_managed_custom_projector(["--lora", "/owner/a.gguf"], "m.gguf", None, "Q8_0")+++def test_resident_paths_need_a_real_cache_snapshot_and_follow_an_omitted_variant(+ monkeypatch, tmp_path+):+ from fastapi import HTTPException+ from types import SimpleNamespace++ import utils.hf_cache_settings as cache_settings++ hub = tmp_path / "hub"+ monkeypatch.setattr(cache_settings, "known_hf_hub_caches", lambda: [hub])+ intent = SimpleNamespace(+ model_identifier = str(hub / "models--unsloth--B-GGUF/snapshots/abc/B-Q4_K_M.gguf"),+ hf_variant = "Q4_K_M",+ extra_args = ("--lora", "/owner/a.gguf"),+ llama_cpp_config = None,+ )+ routes = _managed_with_owner(monkeypatch, intent = intent)+ # The same repo, by id or by its real cache path, with the variant omitted or named.+ routes._refuse_managed_custom_projector(["--lora", "/owner/a.gguf"], "unsloth/B-GGUF")+ routes._refuse_managed_custom_projector(+ ["--lora", "/owner/a.gguf"], "unsloth/B-GGUF", None, "Q4_K_M"+ )+ routes._refuse_managed_custom_projector(["--lora", "/owner/a.gguf"], intent.model_identifier)+ # A look-alike snapshot path in an account workspace is not the resident repo.+ fake = tmp_path / "ws/models--unsloth--B-GGUF/snapshots/x/B.gguf"+ with pytest.raises(HTTPException):+ routes._refuse_managed_custom_projector(["--lora", "/owner/a.gguf"], str(fake))+ # Nor does an owner load from a look-alike path outside the cache name that repo.+ outside = SimpleNamespace(+ **{+ **vars(intent),+ "model_identifier": str(tmp_path / "own/models--unsloth--B-GGUF/snapshots/x/B.gguf"),+ }+ )+ monkeypatch.setattr(+ routes, "get_llama_cpp_backend", lambda: SimpleNamespace(last_load_intent = outside)+ )+ with pytest.raises(HTTPException):+ routes._refuse_managed_custom_projector(["--lora", "/owner/a.gguf"], "unsloth/B-GGUF")+ with pytest.raises(HTTPException):+ routes._refuse_managed_custom_projector(+ ["--lora", "/owner/a.gguf"], "unsloth/B-GGUF", None, "Q8_0"+ )+++def test_inherited_owner_paths_get_the_same_managed_check(monkeypatch, tmp_path):+ # A settings Apply omits llama_extra_args and inherits the resident load's list.+ from fastapi import HTTPException+ from types import SimpleNamespace++ import utils.hf_cache_settings as cache_settings+ from models.inference import LoadRequest++ hub = tmp_path / "hub"+ monkeypatch.setattr(cache_settings, "known_hf_hub_caches", lambda: [hub])+ resident = str(hub / "models--unsloth--B-GGUF/snapshots/abc/B-Q4_K_M.gguf")+ backend = SimpleNamespace(+ extra_args = ["--lora", "/owner/a.gguf"],+ extra_args_source = (resident, "Q4_K_M"),+ last_load_intent = SimpleNamespace(+ model_identifier = resident,+ hf_variant = "Q4_K_M",+ extra_args = ("--lora", "/owner/a.gguf"),+ llama_cpp_config = None,+ ),+ )+ routes = _managed_with_owner(monkeypatch)+ monkeypatch.setattr(routes, "get_llama_cpp_backend", lambda: backend)+ config = SimpleNamespace(is_gguf = True, gguf_variant = "Q4_K_M")++ same = LoadRequest(model_path = "unsloth/B-GGUF")+ assert routes._resolve_inherited_extra_args(same, config, "unsloth/B-GGUF", None) == [+ "--lora",+ "/owner/a.gguf",+ ]+ fake = str(tmp_path / "ws/models--unsloth--B-GGUF/snapshots/x/B-Q4_K_M.gguf")+ with pytest.raises(HTTPException):+ routes._resolve_inherited_extra_args(LoadRequest(model_path = fake), config, fake, None)+++def test_managed_caller_may_resend_the_owners_custom_config(monkeypatch):+ import asyncio+ from fastapi import HTTPException+ from types import SimpleNamespace+ from models.inference import LoadRequest++ owner_ini = {+ "version": 1,+ "mode": "custom",+ "ini": "chat-template-file = /owner/t.jinja\n",+ "section": None,+ }+ routes = _managed_with_owner(monkeypatch, {"m.gguf": {"llama_cpp_config": owner_ini}})+ compiled = SimpleNamespace(argv = ("--chat-template-file", "/owner/t.jinja"))+ backend = SimpleNamespace(prepare_custom_config = lambda intent: compiled, last_load_intent = None)+ monkeypatch.setattr(routes, "get_llama_cpp_backend", lambda: backend)+ monkeypatch.setattr(routes, "_classify_diffusion_gguf", lambda config: False)+ config = SimpleNamespace(is_gguf = True, identifier = "m.gguf")+ request = LoadRequest(model_path = "m.gguf", llama_cpp_config = owner_ini)+ got = asyncio.run(+ routes._preflight_custom_llama_config(request, config, caller_sent_custom = True)+ )+ assert got is compiled+ edited = dict(owner_ini, ini = "chat-template-file = /owner/t.jinja\nctx-size = 4096\n")+ request = LoadRequest(model_path = "m.gguf", llama_cpp_config = edited)+ with pytest.raises(HTTPException) as err:+ asyncio.run(routes._preflight_custom_llama_config(request, config, caller_sent_custom = True))+ assert err.value.status_code == 403