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.

Studio: stop a settings module reading a chat store key before it exists (#8979)

Daniel Hane35b8a844720
diff --git a/studio/frontend/src/features/chat/stores/sidebar-organization-keys.ts b/studio/frontend/src/features/chat/stores/sidebar-organization-keys.ts
new file mode 100644
index 00000000000..eb2c0b692be
--- /dev/null
+++ b/studio/frontend/src/features/chat/stores/sidebar-organization-keys.ts
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+/**
+ * Storage keys for the sidebar organization store, in a module that imports
+ * nothing. The store sits in an import cycle through the chat barrel, so the key
+ * defined there was still in its temporal dead zone when `general-tab.tsx` read
+ * it at module scope, throwing and leaving a white screen on launch. A module
+ * with no imports is always evaluated first, so do not add an import here.
+ */
+
+/** Exported so the preference reset clears the same key the store writes. */
+export const SIDEBAR_ORGANIZATION_STORAGE_KEY = "unsloth_sidebar_organization";
diff --git a/studio/frontend/src/features/chat/stores/sidebar-organization-store.ts b/studio/frontend/src/features/chat/stores/sidebar-organization-store.ts
index ef8624787c5..3fe47cb81e0 100644
--- a/studio/frontend/src/features/chat/stores/sidebar-organization-store.ts
+++ b/studio/frontend/src/features/chat/stores/sidebar-organization-store.ts
@@ -9,8 +9,11 @@ export type SidebarOrganizeBy = "project" | "list";
/** How chat rows are ordered inside whichever list they land in. */
export type SidebarChatSort = "priority" | "updated" | "manual";
-// Exported so the preference reset clears the same key this store writes.
-export const SIDEBAR_ORGANIZATION_STORAGE_KEY = "unsloth_sidebar_organization";
+// Defined in a leaf module and re-exported here so existing importers are
+// unchanged: this store is in an import cycle, so a binding defined here would
+// be readable too late. See sidebar-organization-keys.ts.
+export { SIDEBAR_ORGANIZATION_STORAGE_KEY } from "./sidebar-organization-keys.ts";
+import { SIDEBAR_ORGANIZATION_STORAGE_KEY } from "./sidebar-organization-keys.ts";
// Manual order is per list: dragging a chat in one project must not move it in
// another list showing the same chat. Each list gets its own key.
diff --git a/studio/frontend/src/features/settings/tabs/general-tab.tsx b/studio/frontend/src/features/settings/tabs/general-tab.tsx
index 9942e3cb4cf..6134ded04ab 100644
--- a/studio/frontend/src/features/settings/tabs/general-tab.tsx
+++ b/studio/frontend/src/features/settings/tabs/general-tab.tsx
@@ -13,10 +13,10 @@ import {
import { Input } from "@/components/ui/input";
import { Switch } from "@/components/ui/switch";
import { PermissionModeDropdown, useChatRuntimeStore } from "@/features/chat";
-// Direct import, not via the @/features/chat barrel: the barrel is in an import cycle with this
-// file, so this key would still be in its temporal dead zone when the module-scope list below
-// reads it ("Cannot access ... before initialization"), killing the whole module graph.
-import { SIDEBAR_ORGANIZATION_STORAGE_KEY } from "@/features/chat/stores/sidebar-organization-store";
+// From the keys module, not the barrel or the store: both are in an import cycle with this file,
+// so the key was still in its temporal dead zone when the module-scope list below read it, killing
+// the module graph. The keys module imports nothing, so it is always evaluated first.
+import { SIDEBAR_ORGANIZATION_STORAGE_KEY } from "@/features/chat/stores/sidebar-organization-keys";
import {
LOADED_MODELS_PREFERENCE_KEYS,
setShowLoadedModels,
diff --git a/studio/frontend/tests/module-scope-cycle-safety.test.ts b/studio/frontend/tests/module-scope-cycle-safety.test.ts
new file mode 100644
index 00000000000..4cf58a40088
--- /dev/null
+++ b/studio/frontend/tests/module-scope-cycle-safety.test.ts
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+/**
+ * `general-tab.tsx` reads `SIDEBAR_ORGANIZATION_STORAGE_KEY` at module scope.
+ * While that key lived in `sidebar-organization-store.ts`, which sits in an
+ * import cycle through the chat barrel, the read could hit the temporal dead
+ * zone and throw, unmounting the app: a white screen on launch. Import order
+ * hid it by accident, so the fix (a keys module importing nothing, hence always
+ * evaluated first) is asserted here rather than left to convention.
+ */
+
+import assert from "node:assert/strict";
+import { readFile } from "node:fs/promises";
+import path from "node:path";
+import test from "node:test";
+import { fileURLToPath } from "node:url";
+
+import ts from "typescript";
+
+const SRC = fileURLToPath(new URL("../src", import.meta.url));
+const KEYS = path.join(SRC, "features/chat/stores/sidebar-organization-keys.ts");
+const GENERAL_TAB = path.join(SRC, "features/settings/tabs/general-tab.tsx");
+
+const parse = (file: string, text: string) =>
+ ts.createSourceFile(
+ file,
+ text,
+ ts.ScriptTarget.ESNext,
+ true,
+ file.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
+ );
+
+/** Module specifiers of `import`/`export ... from` declarations. */
+const staticSpecifiers = (file: string, text: string): string[] => {
+ const specifiers: string[] = [];
+ const visit = (node: ts.Node): void => {
+ if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) {
+ const specifier = node.moduleSpecifier;
+ if (specifier && ts.isStringLiteral(specifier)) {
+ specifiers.push(specifier.text);
+ }
+ }
+ ts.forEachChild(node, visit);
+ };
+ ts.forEachChild(parse(file, text), visit);
+ return specifiers;
+};
+
+test("the sidebar organization keys module imports nothing", async () => {
+ // An import here puts it back in the cycle and the white screen comes back.
+ const text = await readFile(KEYS, "utf8");
+ assert.deepEqual(
+ staticSpecifiers(KEYS, text),
+ [],
+ "sidebar-organization-keys.ts must not import anything",
+ );
+ assert.match(text, /export const SIDEBAR_ORGANIZATION_STORAGE_KEY/);
+});
+
+test("general-tab reads the key from the keys module, not the store or the barrel", async () => {
+ const text = await readFile(GENERAL_TAB, "utf8");
+ const source = parse(GENERAL_TAB, text);
+
+ let specifier: string | null = null;
+ const visit = (node: ts.Node): void => {
+ if (ts.isImportDeclaration(node) && node.importClause?.namedBindings) {
+ const bindings = node.importClause.namedBindings;
+ if (
+ ts.isNamedImports(bindings) &&
+ bindings.elements.some(
+ (element) => element.name.text === "SIDEBAR_ORGANIZATION_STORAGE_KEY",
+ ) &&
+ ts.isStringLiteral(node.moduleSpecifier)
+ ) {
+ specifier = node.moduleSpecifier.text;
+ }
+ }
+ ts.forEachChild(node, visit);
+ };
+ ts.forEachChild(source, visit);
+
+ assert.equal(
+ specifier,
+ "@/features/chat/stores/sidebar-organization-keys",
+ "general-tab must not reach the key through the store or the chat barrel; " +
+ "both are in an import cycle with this file",
+ );
+});
+
+test("the key is still read at module scope, so the guard above is load-bearing", async () => {
+ // If this stops being a module-scope read, the two tests above are pointless
+ // and should go.
+ const text = await readFile(GENERAL_TAB, "utf8");
+ const source = parse(GENERAL_TAB, text);
+
+ let readAtModuleScope = false;
+ for (const statement of source.statements) {
+ if (!ts.isVariableStatement(statement)) {
+ continue;
+ }
+ const visit = (node: ts.Node): void => {
+ if (
+ ts.isIdentifier(node) &&
+ node.text === "SIDEBAR_ORGANIZATION_STORAGE_KEY"
+ ) {
+ readAtModuleScope = true;
+ }
+ ts.forEachChild(node, visit);
+ };
+ ts.forEachChild(statement, visit);
+ }
+
+ assert.ok(
+ readAtModuleScope,
+ "general-tab no longer reads the key at module scope; drop this file's guards",
+ );
+});