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.

5.6 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

// Guards that both Train start paths still call the transformers-upgrade gate.

import assert from "node:assert/strict";
import test from "node:test";

import { readText } from "./helpers/kit.ts";

const START_PATHS = [
  "../src/features/training/lib/start-fresh-training-run.ts",
  "../src/features/training/lib/resume-training-run.ts",
] as const;

test("both training start paths consult the transformers-upgrade gate", () => {
  for (const file of START_PATHS) {
    assert.ok(
      readText(file).includes("confirmTrainingTransformersUpgrade"),
      `${file} must consult the transformers-upgrade gate: without it a model whose ` +
        "architecture no installed transformers ships is accepted and then dies at " +
        "model load, with no prompt and no way for the user to act on it",
    );
  }
});

test("the upgrade dialog is raised before the custom-code dialog", () => {
  // Same order as chat: installing changes what the load runs, so upgrade consent comes first.
  for (const file of START_PATHS) {
    const source = readText(file);
    const upgradeAt = source.indexOf("confirmTrainingTransformersUpgrade(");
    const remoteCodeAt = source.indexOf("confirmRemoteCodeIfNeeded(");
    // A missing call indexes to -1, which would satisfy the ordering assertion.
    assert.ok(
      upgradeAt >= 0 && remoteCodeAt >= 0,
      `${file} must run both gates`,
    );
    assert.ok(
      upgradeAt < remoteCodeAt,
      `${file} must raise the upgrade dialog before the custom-code dialog`,
    );
  }
});

test("both gates on a start path inspect the same copy of the model", () => {
  // One resolver per start path so both gates judge the same pinned snapshot.
  for (const [file, resolver] of [
    [
      "../src/features/training/lib/start-fresh-training-run.ts",
      "freshModelCachePin(",
    ],
    [
      "../src/features/training/lib/resume-training-run.ts",
      "resumeModelCachePin(",
    ],
  ] as const) {
    const source = readText(file);
    assert.equal(
      source.split(resolver).length - 1,
      3,
      `${file} must resolve the cache pin once and pass it to both gates`,
    );
  }
});

test("the resume gate names the run it precedes", () => {
  // Without the run id the check cannot tell the install would strand a 4-bit checkpoint.
  const source = readText(
    "../src/features/training/lib/resume-training-run.ts",
  );
  assert.ok(source.includes("resumeRunId"));
});

test("the gate reaches the install through the shared consent dialog", () => {
  const gate = readText(
    "../src/features/training/lib/training-transformers-upgrade.ts",
  );
  assert.ok(gate.includes("confirmTransformersUpgradeIfNeeded"));
  assert.ok(gate.includes("checkTransformersUpgrade"));
});

test("the Configure preview re-asks the check after an install", () => {
  // The store counts completed installs and the hook keys its cache on that count.
  const store = readText(
    "../src/features/transformers-upgrade/stores/transformers-upgrade-dialog-store.ts",
  );
  assert.ok(
    /sidecarGeneration:\s*get\(\)\.sidecarGeneration \+ 1/.test(store),
    "a successful install must advance sidecarGeneration",
  );

  const hook = readText(
    "../src/features/training/hooks/use-training-transformers-upgrade-notice.ts",
  );
  assert.ok(hook.includes("s.sidecarGeneration"));
  assert.ok(
    /upgradeNoticeCacheKey\(\s*sidecarGeneration/.test(hook),
    "the preview cache key must carry the generation, or an install cannot retire it",
  );
});

test("the consent dialog offers the custom-code way out before an install fails", () => {
  // The custom-code fallback must be offered up front, or a QLoRA run has no path to 4-bit.
  const dialog = readText(
    "../src/features/transformers-upgrade/components/transformers-upgrade-dialog.tsx",
  );
  assert.ok(
    dialog.includes("upgradeDialogActions"),
    "the dialog must take its actions from the shared decision, not re-derive them",
  );
  assert.doesNotMatch(
    dialog,
    /phase === "error" && trustRemoteCodeFallback/,
    "the custom-code fallback must not wait for an install to fail first",
  );
  assert.match(
    dialog,
    /<DropdownMenuItem onSelect=\{\(\) => resolve\(true\)\}>\s*Continue with custom code/,
    "beside Update, custom code lives under Other and only resolves the dialog",
  );
  assert.doesNotMatch(
    dialog,
    /trustRemoteCode\s*=/,
    "the dialog must never grant trust itself",
  );
});

test("chat raises the remote-code scan after the upgrade dialog", () => {
  for (const file of [
    "../src/features/chat/shared-composer.tsx",
    "../src/features/chat/hooks/use-chat-model-runtime.ts",
  ]) {
    const source = readText(file);
    const upgradeAt = source.indexOf("confirmTransformersUpgradeIfNeeded({");
    const scanAt = source.indexOf("confirmRemoteCodeIfNeeded({", upgradeAt);
    assert.ok(
      upgradeAt >= 0 && scanAt > upgradeAt,
      `${file} must scan custom code after the upgrade dialog`,
    );
  }
});

test("both start paths carry the upgrade gate's custom-code verdict forward", () => {
  // The upgrade check's trust-remote-code answer must travel, since the scan fallback uses a
  // stored flag that is false on a fresh run.
  for (const file of START_PATHS) {
    const source = readText(file);
    assert.ok(
      source.includes(
        "verdict.requiresTrustRemoteCode = outcome.requiresTrustRemoteCode",
      ),
      `${file} must record the upgrade gate's custom-code verdict`,
    );
    assert.ok(
      source.includes("upgradeRequiresTrustRemoteCode"),
      `${file} must pass that verdict into the custom-code gate`,
    );
  }
});