Skip to content

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.

8.1 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

// Purges name caches by key: never a path, and bulk clear skips caches that re-download models.

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";

import { loadWithStubs } from "./helpers/module-stubs.ts";

type CachesApi = {
  loadCacheBreakdown: (key: string) => Promise<
    {
      path: string;
      repoId?: string;
      repoType?: string;
      cachePath?: string;
      deleteAllowed?: boolean;
    }[]
  >;
  loadCacheInventory: () => Promise<unknown>;
  purgeCaches: (keys: readonly string[]) => Promise<unknown>;
  inventoryFromApi: (value: unknown) => {
    caches: { key: string; sizeBytes: number; optIn: boolean }[];
    totalBytes: number;
    reclaimableBytes: number;
    freeBytes: number | null;
  };
  bulkPurgeKeys: (inventory: unknown) => string[];
};

type Request = { url: string; init: RequestInit | undefined };

function loadApi(responder: (url: string, init?: RequestInit) => Response): {
  api: CachesApi;
  requests: Request[];
} {
  const requests: Request[] = [];
  const api = loadWithStubs<CachesApi>(
    new URL("../src/features/settings/api/caches.ts", import.meta.url),
    {
      "@/features/auth": {
        authFetch: async (url: string, init?: RequestInit) => {
          requests.push({ url, init });
          return responder(url, init);
        },
      },
      "@/lib/format-fastapi-error": {
        readFastApiError: async (_response: Response, fallback: string) =>
          fallback,
      },
    },
  );
  return { api, requests };
}

function json(body: unknown): Response {
  return new Response(JSON.stringify(body), {
    status: 200,
    headers: { "Content-Type": "application/json" },
  });
}

test("breakdown preserves each model copy's action identity and requires an explicit permission boolean", async () => {
  const first = "/active/hub/models--Org--Model";
  const second = "/previous/hub/models--Org--Model";
  const { api, requests } = loadApi(() =>
    json({
      entries: [
        {
          name: "models--Org--Model",
          path: first,
          size_bytes: 30,
          repo_id: "Org/Model",
          repo_type: "model",
          cache_path: first,
          delete_allowed: true,
        },
        {
          name: "models--Org--Model",
          path: second,
          size_bytes: 50,
          repo_id: "Org/Model",
          repo_type: "model",
          cache_path: second,
          delete_allowed: true,
        },
        {
          name: ".locks",
          path: "/locks",
          size_bytes: 1,
          delete_allowed: false,
        },
        {
          name: "unverified",
          path: "/unverified",
          size_bytes: 1,
          delete_allowed: "true",
        },
      ],
    }),
  );
  const rows = await api.loadCacheBreakdown("hf_hub");
  assert.equal(requests[0].url, "/api/settings/caches/hf_hub/breakdown");
  assert.deepEqual(
    rows
      .slice(0, 2)
      .map((row) => [
        row.repoId,
        row.repoType,
        row.cachePath,
        row.deleteAllowed,
      ]),
    [
      ["Org/Model", "model", first, true],
      ["Org/Model", "model", second, true],
    ],
  );
  assert.equal(rows[2].deleteAllowed, false);
  assert.equal(rows[3].deleteAllowed, false);
});

const apiEntry = (
  key: string,
  overrides: Record<string, unknown> = {},
): Record<string, unknown> => ({
  key,
  group: "packages",
  // biome-ignore lint/style/useNamingConvention: API schema
  opt_in: false,
  paths: [`/cache/${key}`],
  // biome-ignore lint/style/useNamingConvention: API schema
  size_bytes: 1000,
  // biome-ignore lint/style/useNamingConvention: API schema
  entry_count: 2,
  present: true,
  purgeable: true,
  // biome-ignore lint/style/useNamingConvention: API schema
  blocked_reason: null,
  ...overrides,
});

const apiInventory = (
  caches: Record<string, unknown>[],
): Record<string, unknown> => ({
  caches,
  // biome-ignore lint/style/useNamingConvention: API schema
  total_bytes: 1000,
  // biome-ignore lint/style/useNamingConvention: API schema
  reclaimable_bytes: 1000,
  // biome-ignore lint/style/useNamingConvention: API schema
  free_bytes: 42,
  // biome-ignore lint/style/useNamingConvention: API schema
  total_disk_bytes: 100,
});

test("a purge posts cache keys and nothing that could be a path", async () => {
  const { api, requests } = loadApi(() =>
    json({
      results: [
        {
          key: "uv",
          // biome-ignore lint/style/useNamingConvention: API schema
          freed_bytes: 5,
          // biome-ignore lint/style/useNamingConvention: API schema
          removed_entries: 1,
          errors: [],
        },
      ],
      // biome-ignore lint/style/useNamingConvention: API schema
      freed_bytes: 5,
      inventory: apiInventory([]),
    }),
  );

  await api.purgeCaches(["uv", "triton"]);

  assert.equal(requests.length, 1);
  assert.equal(requests[0].url, "/api/settings/caches/purge");
  assert.equal(requests[0].init?.method, "POST");
  const body = JSON.parse(String(requests[0].init?.body));
  assert.deepEqual(Object.keys(body), ["keys"]);
  assert.deepEqual(body.keys, ["uv", "triton"]);
});

test("the client offers no way to name a directory", () => {
  const source = readFileSync(
    new URL("../src/features/settings/api/caches.ts", import.meta.url),
    "utf8",
  );
  assert.match(source, /JSON\.stringify\(\{ keys \}\)/);
  assert.doesNotMatch(source, /path:\s*[^;]*JSON\.stringify/);
});

test("a failed purge surfaces the backend's own message", async () => {
  const { api } = loadApi(() => new Response(null, { status: 400 }));
  await assert.rejects(api.purgeCaches(["uv"]), /Failed to clear the caches/);
});

test("the inventory maps the API's snake_case into the app's shape", async () => {
  const { api } = loadApi(() =>
    json(
      apiInventory([
        apiEntry("uv"),
        // biome-ignore lint/style/useNamingConvention: API schema
        apiEntry("hf_hub", { opt_in: true, group: "models" }),
      ]),
    ),
  );
  const inventory = (await api.loadCacheInventory()) as ReturnType<
    CachesApi["inventoryFromApi"]
  >;
  assert.equal(inventory.caches[0].sizeBytes, 1000);
  assert.equal(inventory.caches[0].optIn, false);
  assert.equal(inventory.caches[1].optIn, true);
  assert.equal(inventory.freeBytes, 42);
});

test("a bulk clear leaves out the model cache, the blocked and the empty", () => {
  const { api } = loadApi(() => json({}));
  const inventory = api.inventoryFromApi(
    apiInventory([
      apiEntry("uv"),
      // biome-ignore lint/style/useNamingConvention: API schema
      apiEntry("hf_hub", { opt_in: true }),
      // biome-ignore lint/style/useNamingConvention: API schema
      apiEntry("hf_datasets", { opt_in: true }),
      apiEntry("triton", {
        purgeable: false,
        // biome-ignore lint/style/useNamingConvention: API schema
        blocked_reason: "sits inside the protected folder /outputs",
      }),
      // biome-ignore lint/style/useNamingConvention: API schema
      apiEntry("numba", { present: false, size_bytes: 0, entry_count: 0 }),
      // biome-ignore lint/style/useNamingConvention: API schema
      apiEntry("vllm", { size_bytes: 0, entry_count: 0 }),
    ]),
  );
  assert.deepEqual(api.bulkPurgeKeys(inventory), ["uv"]);
});

test("a cache that measures zero bytes but holds entries is still cleared", () => {
  const { api } = loadApi(() => json({}));
  const inventory = api.inventoryFromApi(
    // biome-ignore lint/style/useNamingConvention: API schema
    apiInventory([apiEntry("triton", { size_bytes: 0, entry_count: 40000 })]),
  );
  assert.deepEqual(api.bulkPurgeKeys(inventory), ["triton"]);
});

test("historical cache occupancy never enables a clear of an empty active cache", () => {
  const { api } = loadApi(() => json({}));
  const inventory = api.inventoryFromApi(
    apiInventory([
      apiEntry("hf_xet", {
        size_bytes: 10000,
        entry_count: 100,
        clear_size_bytes: 0,
        clear_entry_count: 0,
        measured_paths: ["/old/xet"],
        system_disk_bytes: 10000,
      }),
    ]),
  );
  assert.deepEqual(api.bulkPurgeKeys(inventory), []);
});