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: keep Projects page renames and deletes from waiting out a streaming reply (#14634)

Michael Han7bbaa7408c8a
diff --git a/studio/frontend/src/features/chat/projects-page.tsx b/studio/frontend/src/features/chat/projects-page.tsx
index e6385752120..8ea3b295baa 100644
--- a/studio/frontend/src/features/chat/projects-page.tsx
+++ b/studio/frontend/src/features/chat/projects-page.tsx
@@ -98,7 +98,7 @@ import {
listStoredChatThreads,
} from "./utils/chat-history-storage";
import { CHAT_HISTORY_UPDATED_EVENT } from "./api/chat-api";
-import { isCoalescedHistoryEvent } from "./utils/chat-history-revision";
+import { createHistoryRefreshDebounce } from "./utils/chat-history-revision";
import { groupThreads, type SidebarItem } from "./hooks/use-chat-sidebar-items";
import { ProjectMenuItems } from "./components/project-menu-items";
import { SectionNameDialog } from "./components/section-name-dialog";
@@ -268,11 +268,8 @@ export function ProjectsPage() {
openProjectIdsRef.current = openProjectIds;
}, [openProjectIds]);
useEffect(() => {
- let timer: ReturnType<typeof setTimeout> | null = null;
- const refresh = (event: Event) => {
- if (timer !== null) clearTimeout(timer);
- timer = setTimeout(() => {
- timer = null;
+ const debounce = createHistoryRefreshDebounce(
+ () => {
const open = openProjectIdsRef.current;
// A closed project's load may still be in flight; its answer must not refill the cache.
for (const [id, seq] of loadSeqRef.current) {
@@ -284,14 +281,14 @@ export function ProjectsPage() {
return kept;
});
for (const id of open) loadProjectChats(id, true);
- }, isCoalescedHistoryEvent(event)
- ? PROJECT_CHATS_STREAM_REFRESH_DEBOUNCE_MS
- : PROJECT_CHATS_REFRESH_DEBOUNCE_MS);
- };
- window.addEventListener(CHAT_HISTORY_UPDATED_EVENT, refresh);
+ },
+ PROJECT_CHATS_REFRESH_DEBOUNCE_MS,
+ PROJECT_CHATS_STREAM_REFRESH_DEBOUNCE_MS,
+ );
+ window.addEventListener(CHAT_HISTORY_UPDATED_EVENT, debounce.onEvent);
return () => {
- if (timer !== null) clearTimeout(timer);
- window.removeEventListener(CHAT_HISTORY_UPDATED_EVENT, refresh);
+ debounce.cancel();
+ window.removeEventListener(CHAT_HISTORY_UPDATED_EVENT, debounce.onEvent);
};
}, [loadProjectChats]);
diff --git a/studio/frontend/src/features/chat/utils/chat-history-revision.ts b/studio/frontend/src/features/chat/utils/chat-history-revision.ts
index 6b88826ecfb..358dd3db147 100644
--- a/studio/frontend/src/features/chat/utils/chat-history-revision.ts
+++ b/studio/frontend/src/features/chat/utils/chat-history-revision.ts
@@ -20,6 +20,44 @@ export function isCoalescedHistoryEvent(event: Event): boolean {
return (event as CustomEvent<{ coalesce?: boolean }>).detail?.coalesce === true;
}
+/** Debounces `run` on history events: chunk saves wait `streamMs` for a lull, a structural change
+ * `structuralMs`. A chunk save joins a pending structural run instead of pushing it back, or a
+ * generation would hold a rename or delete for its whole length. */
+export function createHistoryRefreshDebounce(
+ run: () => void,
+ structuralMs: number,
+ streamMs: number,
+): { onEvent: (event: Event) => void; cancel: () => void } {
+ let timer: ReturnType<typeof setTimeout> | null = null;
+ let structuralPending = false;
+ const cancel = () => {
+ if (timer !== null) {
+ clearTimeout(timer);
+ }
+ timer = null;
+ structuralPending = false;
+ };
+ return {
+ onEvent(event) {
+ const coalesced = isCoalescedHistoryEvent(event);
+ if (coalesced && structuralPending) {
+ return;
+ }
+ cancel();
+ structuralPending = !coalesced;
+ timer = setTimeout(
+ () => {
+ timer = null;
+ structuralPending = false;
+ run();
+ },
+ coalesced ? streamMs : structuralMs,
+ );
+ },
+ cancel,
+ };
+}
+
function storage(): Storage | null {
// no window under node, and the localStorage getter itself throws in some privacy modes
try {
diff --git a/studio/frontend/tests/history-refresh-debounce.test.ts b/studio/frontend/tests/history-refresh-debounce.test.ts
new file mode 100644
index 00000000000..e0dc979a37b
--- /dev/null
+++ b/studio/frontend/tests/history-refresh-debounce.test.ts
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// The Projects page reloads open project rows on history events. A streamed reply's chunk saves
+// wait for a lull, but must not push back a rename or delete that lands mid-stream.
+
+import assert from "node:assert/strict";
+import test, { mock } from "node:test";
+
+const { createHistoryRefreshDebounce } = await import(
+ "../src/features/chat/utils/chat-history-revision.ts"
+);
+
+const chunk = () => new CustomEvent("x", { detail: { coalesce: true } });
+
+test("chunk saves refresh once on a lull, not per chunk", () => {
+ mock.timers.enable({ apis: ["setTimeout"] });
+ try {
+ let runs = 0;
+ const debounce = createHistoryRefreshDebounce(
+ () => {
+ runs += 1;
+ },
+ 300,
+ 2000,
+ );
+ for (let i = 0; i < 20; i += 1) {
+ debounce.onEvent(chunk());
+ mock.timers.tick(500);
+ }
+ assert.equal(runs, 0, "chunks spaced under the lull keep postponing");
+ mock.timers.tick(2000);
+ assert.equal(runs, 1);
+ } finally {
+ mock.timers.reset();
+ }
+});
+
+test("a structural change during a stream refreshes on its own deadline", () => {
+ mock.timers.enable({ apis: ["setTimeout"] });
+ try {
+ let runs = 0;
+ const debounce = createHistoryRefreshDebounce(
+ () => {
+ runs += 1;
+ },
+ 300,
+ 2000,
+ );
+ debounce.onEvent(chunk());
+ mock.timers.tick(100);
+ debounce.onEvent(new Event("x"));
+ for (let i = 0; i < 3; i += 1) {
+ mock.timers.tick(100);
+ debounce.onEvent(chunk());
+ }
+ assert.equal(runs, 1, "a rename or delete waited for the stream to end");
+ mock.timers.tick(1999);
+ assert.equal(runs, 1);
+ mock.timers.tick(1);
+ assert.equal(
+ runs,
+ 2,
+ "chunks after the structural run still get their lull",
+ );
+ } finally {
+ mock.timers.reset();
+ }
+});
+
+test("cancel drops a pending refresh", () => {
+ mock.timers.enable({ apis: ["setTimeout"] });
+ try {
+ let runs = 0;
+ const debounce = createHistoryRefreshDebounce(
+ () => {
+ runs += 1;
+ },
+ 300,
+ 2000,
+ );
+ debounce.onEvent(new Event("x"));
+ debounce.cancel();
+ mock.timers.tick(5000);
+ assert.equal(runs, 0);
+ } finally {
+ mock.timers.reset();
+ }
+});
diff --git a/studio/frontend/tests/projects-activity-and-polling.test.ts b/studio/frontend/tests/projects-activity-and-polling.test.ts
index b24c7bb8ced..d24b0b89a2f 100644
--- a/studio/frontend/tests/projects-activity-and-polling.test.ts
+++ b/studio/frontend/tests/projects-activity-and-polling.test.ts
@@ -41,7 +41,11 @@ test("exporting every project lists chats once, not once per project", () => {
});
test("streamed chunk saves reload open project rows on a longer lull", () => {
- assert.match(PAGE, /isCoalescedHistoryEvent\(event\)\n\s*\? PROJECT_CHATS_STREAM_REFRESH_DEBOUNCE_MS/);
+ assert.match(
+ PAGE,
+ /PROJECT_CHATS_REFRESH_DEBOUNCE_MS,\n\s*PROJECT_CHATS_STREAM_REFRESH_DEBOUNCE_MS,\n\s*\);/,
+ );
+ assert.match(PAGE, /addEventListener\(CHAT_HISTORY_UPDATED_EVENT, debounce\.onEvent\)/);
});
test("a mount reuses a fresh project list instead of refetching it", () => {
diff --git a/studio/frontend/tests/projects-page-rows.test.ts b/studio/frontend/tests/projects-page-rows.test.ts
index 7e0d8478033..80a63bf4b44 100644
--- a/studio/frontend/tests/projects-page-rows.test.ts
+++ b/studio/frontend/tests/projects-page-rows.test.ts
@@ -30,10 +30,10 @@ test("a project row opens its chats in place", () => {
PAGE,
/listStoredChatThreads\(\{ projectId, includeArchived: false \}\)/,
);
- assert.match(PAGE, /window\.addEventListener\(CHAT_HISTORY_UPDATED_EVENT, refresh\);/);
+ assert.match(PAGE, /window\.addEventListener\(CHAT_HISTORY_UPDATED_EVENT, debounce\.onEvent\);/);
// Debounced, since streaming fires the event per chunk, and open rows reload in place.
- // A chunk save waits for a longer lull than a structural change.
- assert.match(PAGE, /timer = setTimeout\(\(\) => \{[\s\S]*?for \(const id of open\) loadProjectChats\(id, true\);\n\s*\}, isCoalescedHistoryEvent\(event\)\n\s*\? PROJECT_CHATS_STREAM_REFRESH_DEBOUNCE_MS\n\s*: PROJECT_CHATS_REFRESH_DEBOUNCE_MS\);/);
+ // A chunk save waits for a longer lull than a structural change (history-refresh-debounce.test.ts).
+ assert.match(PAGE, /createHistoryRefreshDebounce\([\s\S]*?for \(const id of open\) loadProjectChats\(id, true\);\n\s*\},\n\s*PROJECT_CHATS_REFRESH_DEBOUNCE_MS,\n\s*PROJECT_CHATS_STREAM_REFRESH_DEBOUNCE_MS,\n\s*\);/);
assert.match(PAGE, /if \(!silent\) \{\n\s*setProjectChats\(\(prev\) => \(\{ \.\.\.prev, \[projectId\]: "loading" \}\)\);/);
// A closed project's pending load is invalidated with its cache entry.
assert.match(PAGE, /for \(const \[id, seq\] of loadSeqRef\.current\) \{\n\s*if \(!open\.has\(id\)\) loadSeqRef\.current\.set\(id, seq \+ 1\);/);