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
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Owner-managed account lifecycle. Private roots are retired, never erased."""
import json
import sqlite3
from contextlib import closing, contextmanager
from datetime import datetime, timezone
from pathlib import Path
from fastapi import APIRouter, Depends, HTTPException, Response, status
from auth import policy, storage
from auth.authentication import authenticated_via_api_key, get_current_subject
from models.auth import (
AccountActiveRequest,
AccountListResponse,
AccountModelAccessRequest,
AccountResponse,
AccountSetupResponse,
CreateAccountRequest,
OwnerLoginAliasRequest,
)
from hub.services.models import account_access
from state import active_generations
from storage import sqlite_gate
from utils.account_context import OWNER, AccountContext, run_as
from utils.personalization_settings import PERSONALIZATION_SETTING_KEY
from utils.paths import storage_roots
def _require_ui_session(via_api_key: bool = Depends(authenticated_via_api_key)) -> None:
# A setup code signs in as the account, so an API key or keyless caller must not mint one.
if via_api_key:
raise HTTPException(status_code = 403, detail = "Account management requires a UI session.")
router = APIRouter(
dependencies = [
Depends(get_current_subject),
Depends(policy.require_owner),
Depends(_require_ui_session),
]
)
@contextmanager
def _account_errors():
try:
yield
except LookupError:
raise HTTPException(status_code = 404, detail = "Account not found")
except ValueError as exc:
raise HTTPException(status_code = 400, detail = str(exc))
except sqlite3.IntegrityError:
raise HTTPException(status_code = 409, detail = "Username is unavailable")
def retire_account_roots(account: AccountContext):
"""Signal this account's work, then rename each private root aside (children first, as roots
may nest). Returns a callable that renames them back if a later step fails."""
if account.is_owner or account.account_id == "owner":
raise ValueError("The installation owner cannot be retired")
active_generations.fence(account.account_id)
active_generations.cancel_all(account.account_id)
account_access.retire_resident_shares(account.account_id)
from core.inference.external_provider import retire_account_clients
# Cache is keyed by account, so nothing else would drop these clients.
retire_account_clients(account.account_id)
from core.inference.mcp_client import close_mcp_sessions, invalidate_tool_cache
from core.training.account_jobs import retire_account_jobs
retire_account_jobs(account)
from core.inference.video import generation_account_in_flight, get_video_backend
from core.training.account_jobs import AccountRetirementError
from core.inference.diffusion_engine_router import cancel_generation_for_account
# Signal both media generations before raising, so one delete retry stops them all.
video_active = generation_account_in_flight() == account.account_id
if video_active:
get_video_backend().cancel_generate(expected_account = account.account_id)
from routes.inference import retire_account_image_jobs
retire_account_image_jobs(account.account_id)
image_active = cancel_generation_for_account(account.account_id)
from core.inference import video_restyle
restyle_active = video_restyle.runner.retire(account.account_id)
if video_active or image_active or restyle_active:
raise AccountRetirementError("Media generation is still active; retry deletion")
# A load the account started before its tombstone is torn down here; one after it is refused.
from core.inference import video as video_module
from core.inference.diffusion_engine_router import retire_load_for_account
from routes.inference import retire_account_loads
retire_load_for_account(account.account_id)
video_module.retire_load_for_account(account.account_id)
retire_account_loads(account.account_id)
run_as(account, close_mcp_sessions)
run_as(account, invalidate_tool_cache)
from storage.studio_db import close_wal_keeper_for
close_wal_keeper_for(run_as(account, storage_roots.workspace_root) / "studio.db")
from storage import rag_db
# Built from the workspace like studio.db: rag_db_path() refuses an escaping link, which must not block deletion.
rag_db.close_wal_keeper_for(run_as(account, storage_roots.workspace_root) / "rag" / "rag.db")
from core.inference import file_history
file_history.close_store(run_as(account, storage_roots.account_path, "file_history"))
roots = {root.absolute() for root in run_as(account, storage_roots.managed_account_roots)}
stamp = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%S%fZ")
moved: list[tuple[Path, Path]] = []
def restore() -> None:
# A root that will not come back stays listed, so the caller hears where its data is.
stranded: list[tuple[Path, Path, OSError]] = []
with storage_roots.root_retirement_lock:
for root, destination in reversed(moved):
try:
Path.rename(destination, root)
except OSError as exc:
stranded.append((root, destination, exc))
moved[:] = [(root, destination) for root, destination, _ in stranded]
if stranded:
raise AccountRetirementError(
"Could not restore retired directories; the data remains at "
+ ", ".join(str(destination) for _, destination, _ in stranded)
) from stranded[0][2]
# Same lock as ensure_account_dir: the rename never lands between its check and mkdir.
with storage_roots.root_retirement_lock:
try:
for root in sorted(roots, key = lambda path: len(path.parts), reverse = True):
# Rename a symlink itself; never resolve it into another account's data.
if not root.exists() and not root.is_symlink():
continue
destination = root.with_name(f"{root.name}-deleted-{stamp}")
suffix = 0
while destination.exists() or destination.is_symlink():
suffix += 1
destination = root.with_name(f"{root.name}-deleted-{stamp}-{suffix}")
Path.rename(root, destination)
moved.append((root, destination))
except OSError:
# All or nothing: reactivation restores no roots, so a half-retired account is empty.
restore()
raise
return restore
def _profile_of(account: dict) -> dict:
"""Read-only: get_connection would create and migrate a studio.db per listed account."""
context = (
OWNER
if account["role"] == "owner"
else AccountContext(account["account_id"], account["username"], account["role"])
)
try:
path = run_as(context, storage_roots.studio_db_path)
if not path.is_file():
return {}
with closing(sqlite_gate.connect(f"{path.resolve().as_uri()}?mode=ro", uri = True)) as conn:
row = conn.execute(
"SELECT value_json FROM app_settings WHERE key = ?", (PERSONALIZATION_SETTING_KEY,)
).fetchone()
profile = json.loads(row[0]).get("profile") if row else None
except Exception: # noqa: BLE001 - a missing or unreadable profile just shows the username
return {}
if not isinstance(profile, dict):
return {}
name, avatar = profile.get("displayName"), profile.get("avatarDataUrl")
return {
"display_name": name.strip() if isinstance(name, str) and name.strip() else None,
"avatar_url": avatar if isinstance(avatar, str) and avatar else None,
}
@router.get("", response_model = AccountListResponse)
def list_accounts():
return {
"accounts": [{**account, **_profile_of(account)} for account in storage.list_accounts()]
}
@router.put("/owner/login-alias", response_model = OwnerLoginAliasRequest)
def set_owner_login_alias(payload: OwnerLoginAliasRequest):
with _account_errors():
return {"login_alias": storage.set_owner_login_alias(payload.login_alias)}
@router.post("", response_model = AccountSetupResponse, status_code = status.HTTP_201_CREATED)
def create_account(payload: CreateAccountRequest):
with _account_errors():
return storage.issue_account_setup_code(username = payload.username)
@router.post("/{account_id}/setup-code", response_model = AccountSetupResponse)
def regenerate_setup_code(account_id: str):
with _account_errors():
return storage.issue_account_setup_code(account_id = account_id)
@router.patch("/{account_id}", response_model = AccountResponse)
def set_account_active(account_id: str, payload: AccountActiveRequest):
with _account_errors():
result = storage.set_account_active(account_id, payload.is_active)
if payload.is_active:
# A delete that failed after retiring the jobs left the id tombstoned in-process.
from core.inference.external_provider import restore_account_clients
from core.training.account_jobs import restore_account_jobs
restore_account_jobs(account_id)
restore_account_clients(account_id)
active_generations.lift_fence(account_id)
else:
from core.scheduled_tasks.scheduler import cancel_account_runs
from routes.inference import retire_account_loads
# Fence first, so a request registering after the sweep is cancelled too.
active_generations.fence(account_id)
active_generations.cancel_all(account_id)
from routes.inference import retire_account_image_jobs
retire_account_image_jobs(account_id)
# A scheduled run still waiting for the model gate has not registered yet.
cancel_account_runs(account_id)
retire_account_loads(account_id)
account_access.retire_resident_shares(account_id)
return result
@router.put("/{account_id}/username", response_model = AccountResponse)
def rename_account(account_id: str, payload: CreateAccountRequest):
with _account_errors():
return storage.rename_account(account_id, payload.username)
@router.put("/{account_id}/model-access", response_model = AccountResponse)
def set_account_model_access(account_id: str, payload: AccountModelAccessRequest):
with _account_errors():
result = storage.set_account_model_allowlist(account_id, payload.model_allowlist)
# A shared resident it may no longer use needs a fresh, gated load to join again.
account_access.retire_resident_shares(account_id)
return result
@router.delete("/{account_id}", status_code = status.HTTP_204_NO_CONTENT)
def delete_account(account_id: str):
from core.training.account_jobs import AccountRetirementError
with _account_errors():
try:
storage.delete_account(account_id, retire_account_roots)
except (OSError, AccountRetirementError) as exc:
detail = "Could not retire account files. The account is disabled; retry deletion."
if isinstance(exc, AccountRetirementError):
detail = f"{detail} {exc}"
raise HTTPException(status_code = 409, detail = detail)
return Response(status_code = status.HTTP_204_NO_CONTENT)