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 two Studio windows undoing each other's browser data (#14277)

Nilayba10ee8b077b
diff --git a/studio/frontend/scripts/check-bundle-budget.ts b/studio/frontend/scripts/check-bundle-budget.ts
index dc0e5608d05..eb0991636a9 100644
--- a/studio/frontend/scripts/check-bundle-budget.ts
+++ b/studio/frontend/scripts/check-bundle-budget.ts
@@ -325,8 +325,10 @@ export const BUDGET = {
// Main at dcf5c4009 measures 6,870,312 B raw / 2,068,954 B transfer, already over the line above. Browser panel
// page downloads add 1,468 B raw / 354 B transfer on top (6,871,780 B raw, 2,069,308 B transfer, one machine and
// build, Node 24, 97 chunks). Raised to that build plus the same 160 B raw / 189 B transfer margin.
- transferBytes: 2_069_497,
- rawBytes: 6_871_940,
+ // Browser store sync across windows (#14277) adds 545 B raw / 288 B transfer on top (6,872,325 B raw, 2,069,596 B
+ // transfer merged with main at 4962983b0, Node 24, 97 chunks). Raised to that plus the same margin.
+ transferBytes: 2_069_785,
+ rawBytes: 6_872_485,
};
// The chunk count is reported but not budgeted. Splitting a page out of the entry raises it while lowering the
diff --git a/studio/frontend/src/features/browser/agent/agent-store.ts b/studio/frontend/src/features/browser/agent/agent-store.ts
index a977c97c0c4..efbb73d47cc 100644
--- a/studio/frontend/src/features/browser/agent/agent-store.ts
+++ b/studio/frontend/src/features/browser/agent/agent-store.ts
@@ -3,6 +3,7 @@
import { create } from "zustand";
import { persist } from "zustand/middleware";
+import { syncAcrossWindows } from "@/lib/sync-across-windows";
import type { Question } from "./gate";
import {
type AgentPermissions,
@@ -106,6 +107,7 @@ export const useBrowserAgentStore = create<BrowserAgentState>()(
},
),
);
+syncAcrossWindows(useBrowserAgentStore);
// A new chat's key -> its saved one, so a run that captured the old key still reaches its chat.
const adopted = new Map<string, string>();
diff --git a/studio/frontend/src/features/browser/bookmarks-store.ts b/studio/frontend/src/features/browser/bookmarks-store.ts
index e36156cf1bb..4cec65147b9 100644
--- a/studio/frontend/src/features/browser/bookmarks-store.ts
+++ b/studio/frontend/src/features/browser/bookmarks-store.ts
@@ -4,6 +4,7 @@
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";
import { accountDatabaseName } from "@/lib/account-transition";
+import { syncAcrossWindows } from "@/lib/sync-across-windows";
import { MAX_TITLE_CHARS, MAX_URL_CHARS } from "./history-store";
export type BookmarkFolder = "toolbar" | "other";
@@ -118,6 +119,7 @@ export const useBrowserBookmarksStore = create<BrowserBookmarksState>()(
},
),
);
+syncAcrossWindows(useBrowserBookmarksStore);
export function useBookmarkFor(url: string | null): Bookmark | undefined {
return useBrowserBookmarksStore((state) =>
diff --git a/studio/frontend/src/features/browser/download-sites-store.ts b/studio/frontend/src/features/browser/download-sites-store.ts
index 764d96112fd..de8c659dead 100644
--- a/studio/frontend/src/features/browser/download-sites-store.ts
+++ b/studio/frontend/src/features/browser/download-sites-store.ts
@@ -6,6 +6,7 @@
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";
import { accountDatabaseName } from "@/lib/account-transition";
+import { syncAcrossWindows } from "@/lib/sync-across-windows";
export type DownloadSiteDecision = "allow" | "block";
@@ -33,3 +34,4 @@ export const useDownloadSitesStore = create<DownloadSitesState>()(
},
),
);
+syncAcrossWindows(useDownloadSitesStore);
diff --git a/studio/frontend/src/features/browser/history-store.ts b/studio/frontend/src/features/browser/history-store.ts
index d314f9e643d..fd74ac0e468 100644
--- a/studio/frontend/src/features/browser/history-store.ts
+++ b/studio/frontend/src/features/browser/history-store.ts
@@ -5,6 +5,7 @@ import { create } from "zustand";
import { type StateStorage, createJSONStorage, persist } from "zustand/middleware";
import { useChatRuntimeStore } from "@/features/chat";
import { accountDatabaseName } from "@/lib/account-transition";
+import { syncAcrossWindows } from "@/lib/sync-across-windows";
import { hostOf } from "./address";
import { forgetNativeDownloads } from "./native-downloads";
import { useBrowserPrefsStore } from "./prefs-store";
@@ -56,10 +57,11 @@ function retentionCutoff(): number {
}
/** localStorage with batched writes, since history is one big JSON value; a full storage is ignored. */
-function deferredLocalStorage(): StateStorage {
+function deferredLocalStorage(): StateStorage & { flush: () => void } {
const pending = new Map<string, string>();
let timer: ReturnType<typeof setTimeout> | null = null;
const flush = () => {
+ if (timer !== null) clearTimeout(timer);
timer = null;
for (const [name, value] of pending) {
try {
@@ -70,8 +72,14 @@ function deferredLocalStorage(): StateStorage {
}
pending.clear();
};
- if (typeof window !== "undefined") window.addEventListener("pagehide", flush);
+ if (typeof window !== "undefined") {
+ window.addEventListener("pagehide", flush);
+ window.addEventListener("storage", (event) => {
+ if (event.key !== null) pending.delete(event.key);
+ });
+ }
return {
+ flush,
getItem: (name) => pending.get(name) ?? localStorage.getItem(name),
setItem: (name, value) => {
pending.set(name, value);
@@ -84,6 +92,9 @@ function deferredLocalStorage(): StateStorage {
};
}
+// Removals are written at once: another window's older deferred write landing after them would undo them.
+const historyStorage = deferredLocalStorage();
+
let nextId = 0;
const newId = () => `${Date.now().toString(36)}-${(nextId++).toString(36)}`;
@@ -159,23 +170,37 @@ export const useBrowserHistoryStore = create<BrowserHistoryState>()(
forgetNativeDownloads(dropped);
return { downloads: downloads.slice(0, MAX_DOWNLOADS) };
});
+ historyStorage.flush();
return id;
},
- removeVisit: (id) => set((state) => ({ history: state.history.filter((item) => item.id !== id) })),
- removeVisits: (ids) => set((state) => ({ history: state.history.filter((item) => !ids.has(item.id)) })),
- removeDownload: (id) =>
+ removeVisit: (id) => {
+ set((state) => ({ history: state.history.filter((item) => item.id !== id) }));
+ historyStorage.flush();
+ },
+ removeVisits: (ids) => {
+ set((state) => ({ history: state.history.filter((item) => !ids.has(item.id)) }));
+ historyStorage.flush();
+ },
+ removeDownload: (id) => {
set((state) => {
const nativeId = state.downloads.find((item) => item.id === id)?.nativeId;
if (nativeId) forgetNativeDownloads([nativeId]);
return { downloads: state.downloads.filter((item) => item.id !== id) };
- }),
- clearHistory: () => set({ history: [], icons: {} }),
- clearDownloads: () =>
+ });
+ historyStorage.flush();
+ },
+ clearHistory: () => {
+ set({ history: [], icons: {} });
+ historyStorage.flush();
+ },
+ clearDownloads: () => {
set((state) => {
// The app's registry is shared by every account; forget only this one's.
forgetNativeDownloads(state.downloads.flatMap((item) => (item.nativeId ? [item.nativeId] : [])));
return { downloads: [] };
- }),
+ });
+ historyStorage.flush();
+ },
pruneHistory: () =>
set((state) => {
const cutoff = retentionCutoff();
@@ -188,10 +213,11 @@ export const useBrowserHistoryStore = create<BrowserHistoryState>()(
// Per account: a write still deferred at a switch lands under the account that made it.
name: accountDatabaseName("unsloth_browser_history"),
version: 1,
- storage: createJSONStorage(deferredLocalStorage),
+ storage: createJSONStorage(() => historyStorage),
},
),
);
+syncAcrossWindows(useBrowserHistoryStore);
const PRUNE_INTERVAL_MS = 60 * 60 * 1000;
diff --git a/studio/frontend/src/features/browser/pinned-pages-store.ts b/studio/frontend/src/features/browser/pinned-pages-store.ts
index 6f2c257ec1c..effb08adb5e 100644
--- a/studio/frontend/src/features/browser/pinned-pages-store.ts
+++ b/studio/frontend/src/features/browser/pinned-pages-store.ts
@@ -7,6 +7,7 @@
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";
import { accountDatabaseName } from "@/lib/account-transition";
+import { syncAcrossWindows } from "@/lib/sync-across-windows";
import { MAX_TITLE_CHARS, MAX_URL_CHARS } from "./history-store";
export type PinnedPage = {
@@ -63,3 +64,4 @@ export const usePinnedPagesStore = create<PinnedPagesState>()(
},
),
);
+syncAcrossWindows(usePinnedPagesStore);
diff --git a/studio/frontend/src/features/browser/prefs-store.ts b/studio/frontend/src/features/browser/prefs-store.ts
index 0d186ef8adc..68a2abf3c7e 100644
--- a/studio/frontend/src/features/browser/prefs-store.ts
+++ b/studio/frontend/src/features/browser/prefs-store.ts
@@ -114,6 +114,11 @@ export const useBrowserPrefsStore = create<BrowserPrefsState>()(
},
),
);
+if (typeof window !== "undefined") {
+ window.addEventListener("storage", (event) => {
+ if (event.key === useBrowserPrefsStore.persist.getOptions().name) void useBrowserPrefsStore.persist.rehydrate();
+ });
+}
/** The zoom a page is at unless the reader zooms it. */
export function defaultZoom(): number {
diff --git a/studio/frontend/src/lib/sync-across-windows.ts b/studio/frontend/src/lib/sync-across-windows.ts
new file mode 100644
index 00000000000..6e559dc4137
--- /dev/null
+++ b/studio/frontend/src/lib/sync-across-windows.ts
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+type PersistedStore = { persist: { getOptions: () => { name?: string }; rehydrate: () => unknown } };
+
+export function syncAcrossWindows(store: PersistedStore): void {
+ if (typeof window === "undefined") return;
+ window.addEventListener("storage", (event) => {
+ if (event.key === store.persist.getOptions().name) void store.persist.rehydrate();
+ });
+}
diff --git a/studio/frontend/tests/browser-history-account.test.ts b/studio/frontend/tests/browser-history-account.test.ts
index 320aa529d8d..ae179773495 100644
--- a/studio/frontend/tests/browser-history-account.test.ts
+++ b/studio/frontend/tests/browser-history-account.test.ts
@@ -14,7 +14,7 @@ const localStorage = {
};
Object.assign(globalThis, {
localStorage,
- window: { localStorage, location: { protocol: "http:" }, addEventListener: (_: string, flush: () => void) => flushes.push(flush) },
+ window: { localStorage, location: { protocol: "http:" }, addEventListener: (type: string, flush: () => void) => type === "pagehide" && flushes.push(flush) },
});
register("./helpers/browser-store-resolver.mjs", import.meta.url);
diff --git a/studio/frontend/tests/browser-sync-across-windows.test.ts b/studio/frontend/tests/browser-sync-across-windows.test.ts
new file mode 100644
index 00000000000..a01e99ac4c1
--- /dev/null
+++ b/studio/frontend/tests/browser-sync-across-windows.test.ts
@@ -0,0 +1,137 @@
+// 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 { register } from "node:module";
+import { test } from "node:test";
+
+const data = new Map<string, string>([["unsloth.browser-account.v1", "account:b1:bob"]]);
+const listeners = new Map<string, ((event: { key: string | null }) => void)[]>();
+const localStorage = {
+ getItem: (key: string) => data.get(key) ?? null,
+ setItem: (key: string, value: string) => void data.set(key, value),
+ removeItem: (key: string) => void data.delete(key),
+};
+Object.assign(globalThis, {
+ localStorage,
+ window: {
+ localStorage,
+ location: { protocol: "http:" },
+ addEventListener: (type: string, listener: (event: { key: string | null }) => void) =>
+ listeners.set(type, [...(listeners.get(type) ?? []), listener]),
+ },
+});
+
+register("./helpers/browser-store-resolver.mjs", import.meta.url);
+const { useBrowserHistoryStore } = await import("../src/features/browser/history-store.ts");
+const { useBrowserPrefsStore } = await import("../src/features/browser/prefs-store.ts");
+const { useBrowserBookmarksStore } = await import("../src/features/browser/bookmarks-store.ts");
+const { usePinnedPagesStore } = await import("../src/features/browser/pinned-pages-store.ts");
+const { useDownloadSitesStore } = await import("../src/features/browser/download-sites-store.ts");
+const { useBrowserAgentStore, patchAgentChat } = await import("../src/features/browser/agent/agent-store.ts");
+
+function stored(key: string): Record<string, unknown> {
+ return JSON.parse(data.get(key) ?? "{}").state ?? {};
+}
+
+function otherWindowWrites(key: string, change: (state: Record<string, unknown>) => Record<string, unknown>) {
+ const saved = JSON.parse(data.get(key) ?? "{}");
+ data.set(key, JSON.stringify({ ...saved, state: change(saved.state ?? {}) }));
+ for (const listener of listeners.get("storage") ?? []) listener({ key });
+}
+
+const flush = () => {
+ for (const listener of listeners.get("pagehide") ?? []) listener({ key: null });
+};
+
+test("a setting turned off in another window stays off after this window changes another one", () => {
+ useBrowserPrefsStore.getState().setShowFullUrl(false);
+ otherWindowWrites("unsloth_browser_prefs", (state) => ({ ...state, saveHistory: false }));
+ useBrowserPrefsStore.getState().setShowFullUrl(true);
+ assert.equal(stored("unsloth_browser_prefs").saveHistory, false);
+ assert.equal(stored("unsloth_browser_prefs").showFullUrl, true);
+ useBrowserHistoryStore.getState().recordVisit("https://example.org/", "Org");
+ assert.equal(useBrowserHistoryStore.getState().history.length, 0);
+ useBrowserPrefsStore.getState().setSaveHistory(true);
+});
+
+test("history cleared in another window does not come back from this window's visits", () => {
+ useBrowserHistoryStore.getState().recordVisit("https://example.com/", "Example");
+ flush();
+ otherWindowWrites("unsloth_browser_history:b1", (state) => ({ ...state, history: [], icons: {} }));
+ useBrowserHistoryStore.getState().recordVisit("https://httpbin.org/html", "httpbin");
+ flush();
+ const urls = (stored("unsloth_browser_history:b1").history as { url: string }[]).map((visit) => visit.url);
+ assert.deepEqual(urls, ["https://httpbin.org/html"]);
+});
+
+test("a write another window makes wins over this window's unsaved one", () => {
+ useBrowserHistoryStore.getState().recordVisit("https://example.net/", "Net");
+ otherWindowWrites("unsloth_browser_history:b1", (state) => ({ ...state, history: [] }));
+ flush();
+ assert.deepEqual(stored("unsloth_browser_history:b1").history, []);
+});
+
+test("bookmarks, pinned pages and download site rules saved in two windows are all kept", () => {
+ useBrowserBookmarksStore.getState().addBookmark("https://example.com/", "Example");
+ otherWindowWrites("unsloth_browser_bookmarks:b1", (state) => ({
+ ...state,
+ bookmarks: [{ id: "x", url: "https://httpbin.org/", title: "httpbin", folder: "toolbar", addedAt: 1 }],
+ }));
+ useBrowserBookmarksStore.getState().addBookmark("https://example.org/", "Org");
+ const bookmarks = (stored("unsloth_browser_bookmarks:b1").bookmarks as { url: string }[]).map((b) => b.url);
+ assert.deepEqual(bookmarks, ["https://httpbin.org/", "https://example.org/"]);
+
+ usePinnedPagesStore.getState().pin("https://example.com/", "Example");
+ otherWindowWrites("unsloth_browser_pinned_pages:b1", (state) => ({
+ ...state,
+ pages: [{ id: "p", url: "https://httpbin.org/", title: "httpbin", pinnedAt: 1 }],
+ }));
+ usePinnedPagesStore.getState().pin("https://example.org/", "Org");
+ const pages = (stored("unsloth_browser_pinned_pages:b1").pages as { url: string }[]).map((p) => p.url);
+ assert.deepEqual(pages, ["https://httpbin.org/", "https://example.org/"]);
+
+ useDownloadSitesStore.getState().setSite("https://example.com", "allow");
+ otherWindowWrites("unsloth_browser_download_sites:b1", () => ({ sites: { "https://httpbin.org": "block" } }));
+ useDownloadSitesStore.getState().setSite("https://example.org", "allow");
+ assert.deepEqual(stored("unsloth_browser_download_sites:b1").sites, {
+ "https://httpbin.org": "block",
+ "https://example.org": "allow",
+ });
+});
+
+test("the agent's history access set in another window survives this one's changes, and chats stay", () => {
+ patchAgentChat("chat-1", { tabId: "tab-1" });
+ useBrowserAgentStore.getState().setShowCursor(true);
+ otherWindowWrites("unsloth_browser_agent", (state) => ({ ...state, historyAccess: "never" }));
+ useBrowserAgentStore.getState().setShowCursor(false);
+ assert.equal(stored("unsloth_browser_agent").historyAccess, "never");
+ assert.equal(useBrowserAgentStore.getState().historyAccess, "never");
+ assert.equal(useBrowserAgentStore.getState().chats["chat-1"]?.tabId, "tab-1");
+});
+
+test("clearing or removing history is saved at once, so another window's queued visit cannot bring it back", () => {
+ const key = "unsloth_browser_history:b1";
+ const urls = () => (stored(key).history as { url: string }[]).map((visit) => visit.url).sort();
+ useBrowserHistoryStore.getState().recordVisit("https://example.com/", "Example");
+ useBrowserHistoryStore.getState().recordVisit("https://example.org/", "Org");
+ flush();
+ useBrowserHistoryStore.getState().recordVisit("https://example.net/", "Net");
+ assert.deepEqual(urls(), ["https://example.com/", "https://example.org/"]);
+ const org = useBrowserHistoryStore.getState().history.find((visit) => visit.url === "https://example.org/");
+ useBrowserHistoryStore.getState().removeVisit(org!.id);
+ assert.deepEqual(urls(), ["https://example.com/", "https://example.net/"]);
+ useBrowserHistoryStore.getState().clearHistory();
+ assert.deepEqual(urls(), []);
+});
+
+test("a finished download is saved at once, so another window's history write does not drop it", () => {
+ const key = "unsloth_browser_history:b1";
+ const id = useBrowserHistoryStore.getState().recordDownload(
+ { name: "report.pdf", url: "https://example.com/report.pdf", contentType: "application/pdf", size: 10 },
+ false,
+ );
+ otherWindowWrites(key, (state) => ({ ...state, history: [] }));
+ assert.ok(useBrowserHistoryStore.getState().downloads.some((item) => item.id === id));
+ assert.ok((stored(key).downloads as { id: string }[]).some((item) => item.id === id));
+});