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 each chat switch from keeping the previous thread alive (#17498)

Daniel Han4e50fbc868eb
diff --git a/studio/frontend/src/lib/surface-registry.ts b/studio/frontend/src/lib/surface-registry.ts
index 0fae25d0213..f4b8f422d27 100644
--- a/studio/frontend/src/lib/surface-registry.ts
+++ b/studio/frontend/src/lib/surface-registry.ts
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-import { type Ref, type RefCallback, useCallback } from "react";
+import { type Ref, type RefCallback, useMemo } from "react";
// Shortcut checks read the elements that register here plus whatever is outside the app root
// (third-party portals), instead of walking a long chat's whole document on every press.
@@ -118,18 +118,53 @@ function attachRef<T>(ref: Ref<T> | undefined, value: T): () => void {
return () => undefined;
}
+/** Undoes `attach` once, via the returned cleanup or `ref(null)`: use-composed-ref (react-textarea-autosize)
+ * drops cleanups, and ignoring its `ref(null)` leaked the composer on every chat switch. */
+function undoableRef<T>(attach: (element: T) => () => void): RefCallback<T> {
+ let held: (() => void) | null = null;
+ return (element: T | null) => {
+ held?.();
+ if (element === null) return;
+ const detach = attach(element);
+ const undo = () => {
+ if (held !== undo) return;
+ held = null;
+ detach();
+ };
+ held = undo;
+ return undo;
+ };
+}
+
/** One ref feeding several, so a primitive's own ref and a caller's both see the element. */
export function useMergedRefs<T>(
...refs: (Ref<T> | undefined)[]
): RefCallback<T> {
// biome-ignore lint/correctness/useExhaustiveDependencies: the refs are the dependencies.
- return useCallback((element: T | null) => {
- if (element === null) return;
- const detach = refs.map((ref) => attachRef(ref, element));
+ return useMemo(
+ () =>
+ undoableRef((element: T) => {
+ const detach = refs.map((ref) => attachRef(ref, element));
+ return () => {
+ for (const undo of detach) undo();
+ };
+ }),
+ refs,
+ );
+}
+
+export function surfaceRef<T extends Element>(
+ key: string,
+ forwarded?: Ref<T>,
+): RefCallback<T> {
+ return undoableRef((element: T) => {
+ const unregister = registerSurface(key, element);
+ const detach = attachRef(forwarded, element);
return () => {
- for (const undo of detach) undo();
+ unregister();
+ detach();
};
- }, refs);
+ });
}
/** A ref that registers the element under `key` while mounted and still reaches `forwarded`. */
@@ -137,16 +172,5 @@ export function useSurfaceRef<T extends Element>(
key: string,
forwarded?: Ref<T>,
): RefCallback<T> {
- return useCallback(
- (element: T | null) => {
- if (element === null) return;
- const unregister = registerSurface(key, element);
- const detach = attachRef(forwarded, element);
- return () => {
- unregister();
- detach();
- };
- },
- [key, forwarded],
- );
+ return useMemo(() => surfaceRef(key, forwarded), [key, forwarded]);
}
diff --git a/studio/frontend/tests/surface-ref-null-detach.test.ts b/studio/frontend/tests/surface-ref-null-detach.test.ts
new file mode 100644
index 00000000000..1216803a9c4
--- /dev/null
+++ b/studio/frontend/tests/surface-ref-null-detach.test.ts
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import { registerBundlerResolver } from "./helpers/kit.ts";
+
+registerBundlerResolver();
+
+const { surfaceRef, trackedSurfaces, trustSurfaceRegistry } = await import(
+ "../src/lib/surface-registry.ts"
+);
+
+const KEY = "test-surface";
+const SELECTOR = ".test-surface";
+
+function fakeElement(name: string) {
+ return { name, isConnected: true, matches: () => true } as unknown as Element;
+}
+
+(globalThis as { document?: unknown }).document ??= {};
+trustSurfaceRegistry({
+ isConnected: true,
+ parentElement: null,
+} as unknown as Element);
+
+function registered(): Element[] {
+ return trackedSurfaces(KEY, SELECTOR) ?? [];
+}
+
+test("a ref(null) detach unregisters and clears the forwarded ref", () => {
+ const forwarded = { current: null as Element | null };
+ const ref = surfaceRef(KEY, forwarded);
+ const first = fakeElement("first");
+ ref(first);
+ assert.deepEqual(registered(), [first]);
+ assert.equal(forwarded.current, first);
+ ref(null);
+ assert.deepEqual(registered(), []);
+ assert.equal(forwarded.current, null);
+});
+
+test("attaching a new element without a detach releases the old one", () => {
+ const ref = surfaceRef(KEY);
+ const a = fakeElement("a");
+ const b = fakeElement("b");
+ ref(a);
+ ref(b);
+ assert.deepEqual(registered(), [b]);
+ ref(null);
+ assert.deepEqual(registered(), []);
+});
+
+test("the returned cleanup still works and undoes only once", () => {
+ const calls: (Element | null)[] = [];
+ const ref = surfaceRef(KEY, (value: Element | null) => {
+ calls.push(value);
+ });
+ const el = fakeElement("el");
+ const cleanup = ref(el);
+ assert.equal(typeof cleanup, "function");
+ (cleanup as () => void)();
+ assert.deepEqual(registered(), []);
+ ref(null);
+ (cleanup as () => void)();
+ assert.deepEqual(calls, [el, null]);
+});
+
+test("another holder of the same element keeps it registered", () => {
+ const el = fakeElement("shared");
+ const one = surfaceRef(KEY);
+ const two = surfaceRef(KEY);
+ one(el);
+ two(el);
+ one(null);
+ assert.deepEqual(registered(), [el]);
+ two(null);
+ assert.deepEqual(registered(), []);
+});