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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
fix(studio): reconcile externally updated saved assistant messages (#11500)
Sourav Rajvi5ebf52bf7612
diff --git a/studio/frontend/src/features/chat/runtime-provider.tsx b/studio/frontend/src/features/chat/runtime-provider.tsxindex 0817e5e0f95..d281e1206d8 100644--- a/studio/frontend/src/features/chat/runtime-provider.tsx+++ b/studio/frontend/src/features/chat/runtime-provider.tsx@@ -166,6 +166,11 @@ import {shouldPreserveGenerationMetadata,subscribeGenerationRecoveryTriggers,} from "./utils/chat-generation-recovery";+import {+ beginSavedHistoryReconciliation,+ isSavedHistoryReconciliationSuperseded,+ reconcileOrdinarySavedMessagesInView,+} from "./utils/saved-history-reconciliation";import { createGenerationToolRecovery } from "./utils/generation-tool-recovery";import { mergeContextTruncation } from "./utils/context-truncation";import { registerLiveThreadView } from "./utils/live-thread-head";@@ -189,6 +194,7 @@ import {getStoredChatThreadReadResult,isExpectedBackgroundChatStorageError,listStoredChatMessages,+ readStoredChatMessages,listStoredChatThreads,markThreadIncognito,saveStoredChatMessage,@@ -2263,8 +2269,25 @@ function useStudioRuntimeAdapters(const recoverCurrentThread = () => {const remoteId = aui.threadListItem().getState().remoteId;if (!remoteId) return;- void listStoredChatMessages(remoteId)- .then((messages) => {+ const generation = beginSavedHistoryReconciliation(remoteId);+ void readStoredChatMessages(remoteId)+ .then(({ messages, fromBackend }) => {+ if (isSavedHistoryReconciliationSuperseded(remoteId, generation)) {+ return;+ }+ if (aui.threadListItem().getState().remoteId !== remoteId) {+ return;+ }+ // A legacy browser copy served during an outage is older, not an external update.+ if (fromBackend) {+ reconcileOrdinarySavedMessagesInView(aui, remoteId, messages, {+ editingMessageId:+ useChatRuntimeStore.getState().editingMessageId ?? null,+ });+ }+ if (isSavedHistoryReconciliationSuperseded(remoteId, generation)) {+ return;+ }for (const message of messages) {if (message.role === "assistant" &&diff --git a/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts b/studio/frontend/src/features/chat/utils/chat-generation-recovery.tsindex 3f3912d16f6..c5eb0002c14 100644--- a/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts+++ b/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts@@ -720,12 +720,19 @@ export function subscribeGenerationRecoveryTriggers(recover();}};+ const onFocus = () => {+ if (documentTarget.visibilityState !== "hidden") {+ recover();+ }+ };windowTarget.addEventListener("online", recover);windowTarget.addEventListener("pageshow", recover);+ windowTarget.addEventListener("focus", onFocus);documentTarget.addEventListener("visibilitychange", onVisible);return () => {windowTarget.removeEventListener("online", recover);windowTarget.removeEventListener("pageshow", recover);+ windowTarget.removeEventListener("focus", onFocus);documentTarget.removeEventListener("visibilitychange", onVisible);};}diff --git a/studio/frontend/src/features/chat/utils/chat-history-storage.ts b/studio/frontend/src/features/chat/utils/chat-history-storage.tsindex 92e84a80f36..9c139f1b5d4 100644--- a/studio/frontend/src/features/chat/utils/chat-history-storage.ts+++ b/studio/frontend/src/features/chat/utils/chat-history-storage.ts@@ -808,8 +808,15 @@ function inheritedMessageIds(export async function listStoredChatMessages(threadId: string,): Promise<MessageRecord[]> {- if (isThreadIncognito(threadId)) return [];- if (isChatThreadDeleted(threadId)) return [];+ return (await readStoredChatMessages(threadId)).messages;+}++/** `fromBackend` is false when the backend read failed and the legacy browser copy was returned. */+export async function readStoredChatMessages(+ threadId: string,+): Promise<{ messages: MessageRecord[]; fromBackend: boolean }> {+ if (isThreadIncognito(threadId)) return { messages: [], fromBackend: false };+ if (isChatThreadDeleted(threadId)) return { messages: [], fromBackend: false };const legacyMessages = await readLegacyStore(() => db.messages.where("threadId").equals(threadId).toArray(),[] as MessageRecord[],@@ -836,22 +843,26 @@ export async function listStoredChatMessages((backendMessages.length === 0 && legacyMessages.length > 0),});if (legacyMessages.length > 0 && merged.shouldSync) {- return syncChatMessages(threadId, merged.messages, {+ const messages = await syncChatMessages(threadId, merged.messages, {pruneMissing: false,}).catch(() => merged.messages);+ return { messages, fromBackend: true };}- return merged.messages;+ return { messages: merged.messages, fromBackend: true };}if (backendMessages &&isLegacyChatImportDone() &&legacyMessages.length === 0) {- return [];+ return { messages: [], fromBackend: true };}- return legacyMessages.filter(- (message) => !isChatThreadDeleted(message.threadId),- );+ return {+ messages: legacyMessages.filter(+ (message) => !isChatThreadDeleted(message.threadId),+ ),+ fromBackend: false,+ };}export async function getStoredChatMessage(diff --git a/studio/frontend/src/features/chat/utils/saved-history-reconciliation.ts b/studio/frontend/src/features/chat/utils/saved-history-reconciliation.tsnew file mode 100644index 00000000000..4e5a06a7392--- /dev/null+++ b/studio/frontend/src/features/chat/utils/saved-history-reconciliation.ts@@ -0,0 +1,163 @@+// 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 { ExportedMessageRepository, ThreadMessage } from "@assistant-ui/react";++import type { MessageRecord } from "../types";+import { restoredAssistantStatus } from "./continuation";+import {+ generationNeedsRecovery,+ generationRawContent,+ isLiveGenerationRun,+ recoveredContentToImport,+} from "./chat-generation-recovery";++const refreshGenerations = new Map<string, number>();++export function beginSavedHistoryReconciliation(threadId: string): number {+ const generation = (refreshGenerations.get(threadId) ?? 0) + 1;+ refreshGenerations.set(threadId, generation);+ return generation;+}++export function isSavedHistoryReconciliationSuperseded(+ threadId: string,+ generation: number,+): boolean {+ return refreshGenerations.get(threadId) !== generation;+}++function contentProjectionKey(content: unknown): string {+ const { raw, carried } = generationRawContent(content);+ return `${raw}\0${JSON.stringify(carried)}`;+}++function runtimeCustom(+ message: ThreadMessage,+): Record<string, unknown> {+ const metadata = message.metadata as { custom?: Record<string, unknown> } | undefined;+ return metadata?.custom ?? {};+}++export function shouldReconcileOrdinaryStoredAssistant(options: {+ stored: MessageRecord;+ runtime: ThreadMessage;+ editingMessageId: string | null;+}): boolean {+ const { stored, runtime, editingMessageId } = options;+ if (stored.role !== "assistant" || runtime.role !== "assistant") {+ return false;+ }+ if (editingMessageId === stored.id) {+ return false;+ }+ if (runtime.status?.type === "running") {+ return false;+ }++ const storedMeta = (stored.metadata ?? {}) as Record<string, unknown>;+ const storedRunId = storedMeta.generationRunId;+ if (typeof storedRunId === "string") {+ if (isLiveGenerationRun(storedRunId)) {+ return false;+ }+ if (generationNeedsRecovery(storedMeta)) {+ return false;+ }+ }++ const runtimeMeta = runtimeCustom(runtime);+ const runtimeRunId = runtimeMeta.generationRunId;+ if (typeof runtimeRunId === "string") {+ if (isLiveGenerationRun(runtimeRunId)) {+ return false;+ }+ if (generationNeedsRecovery(runtimeMeta)) {+ return false;+ }+ }++ const storedKey = contentProjectionKey(stored.content);+ const runtimeKey = contentProjectionKey(runtime.content);+ return storedKey !== runtimeKey;+}++export function reconcileOrdinarySavedMessagesInExport(+ exported: ExportedMessageRepository,+ storedMessages: readonly MessageRecord[],+ options: { editingMessageId: string | null },+): { messages: ExportedMessageRepository["messages"]; changed: boolean } {+ const storedById = new Map(storedMessages.map((message) => [message.id, message]));+ let changed = false;+ const messages = exported.messages.map((item) => {+ const stored = storedById.get(item.message.id);+ if (!stored) {+ return item;+ }+ if (+ !shouldReconcileOrdinaryStoredAssistant({+ stored,+ runtime: item.message,+ editingMessageId: options.editingMessageId,+ })+ ) {+ return item;+ }++ const imported = recoveredContentToImport(item.message.content, stored.content);+ if (contentProjectionKey(imported) === contentProjectionKey(item.message.content)) {+ return item;+ }++ changed = true;+ const custom = runtimeCustom(item.message);+ const status = restoredAssistantStatus({ custom });+ return {+ ...item,+ message: {+ ...item.message,+ content: imported as ThreadMessage["content"],+ status,+ } as ThreadMessage,+ };+ });++ return { messages, changed };+}++type ThreadImportExport = {+ export: () => ExportedMessageRepository;+ import: (data: ExportedMessageRepository) => void;+};++type ReconcileView = {+ threadListItem: () => { getState: () => { remoteId?: string | null } };+ thread: () => ThreadImportExport;+};++export function reconcileOrdinarySavedMessagesInView(+ view: ReconcileView,+ threadId: string,+ storedMessages: readonly MessageRecord[],+ options: { editingMessageId: string | null },+): boolean {+ if (view.threadListItem().getState().remoteId !== threadId) {+ return false;+ }+ try {+ const thread = view.thread();+ const exported = thread.export();+ const { messages, changed } = reconcileOrdinarySavedMessagesInExport(+ exported,+ storedMessages,+ options,+ );+ if (!changed) {+ return false;+ }+ thread.import({ ...exported, messages });+ return true;+ } catch {+ return false;+ }+}diff --git a/studio/frontend/tests/saved-history-legacy-fallback.test.ts b/studio/frontend/tests/saved-history-legacy-fallback.test.tsnew file mode 100644index 00000000000..e234a261e17--- /dev/null+++ b/studio/frontend/tests/saved-history-legacy-fallback.test.ts@@ -0,0 +1,101 @@+// 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 type { MessageRecord } from "../src/features/chat/types";+import { loadWithStubs } from "./helpers/module-stubs.ts";++type Storage = {+ readStoredChatMessages: (+ threadId: string,+ ) => Promise<{ messages: MessageRecord[]; fromBackend: boolean }>;+};++class StubWriteCoordinator {+ closeAdmission(): () => void {+ return () => {};+ }+ confirmFinalState(): void {}+ idsRequiringFence(): string[] {+ return [];+ }+}++const legacyRow: MessageRecord = {+ id: "assistant-1",+ threadId: "thread-1",+ parentId: "user-1",+ role: "assistant",+ content: [{ type: "text", text: "Obsolete browser copy" }],+ createdAt: 1,+};++function loadStorage(backend: {+ getChatThread: () => Promise<unknown>;+ listChatMessages: () => Promise<MessageRecord[]>;+}): Storage {+ return loadWithStubs<Storage>(+ new URL(+ "../src/features/chat/utils/chat-history-storage.ts",+ import.meta.url,+ ),+ {+ "../api/chat-api": {+ ChatThreadDeletedError: class extends Error {},+ getChatThread: backend.getChatThread,+ listChatMessages: backend.listChatMessages,+ notifyChatHistoryUpdated: () => {},+ },+ "../db": {+ DEXIE_DB_NAME: "unsloth-chat",+ db: {+ threads: {},+ messages: {+ where: () => ({ equals: () => ({ toArray: async () => [legacyRow] }) }),+ },+ transaction: async () => undefined,+ },+ },+ "./chat-thread-tombstones": {+ isChatThreadDeleted: () => false,+ markChatThreadDeleted: () => {},+ markChatThreadsDeleted: () => {},+ },+ "./thread-record-write-coordinator": {+ ThreadRecordWriteCoordinator: StubWriteCoordinator,+ },+ "../stores/fork-boundary-store": { setForkBoundary: () => {} },+ },+ );+}++test("a failed backend read served from the legacy copy is not a backend read", async () => {+ const storage = loadStorage({+ getChatThread: async () => {+ throw new Error("offline");+ },+ listChatMessages: async () => {+ throw new Error("offline");+ },+ });+ const { messages, fromBackend } = await storage.readStoredChatMessages("thread-1");+ assert.deepEqual(messages.map((m) => m.id), ["assistant-1"]);+ assert.equal(fromBackend, false);+});++test("a successful backend read is a backend read", async () => {+ const storage = loadStorage({+ getChatThread: async () => ({ id: "thread-1" }),+ listChatMessages: async () => [+ { ...legacyRow, content: [{ type: "text", text: "Server copy" }] },+ ],+ });+ const { messages, fromBackend } = await storage.readStoredChatMessages("thread-1");+ assert.equal(fromBackend, true);+ assert.equal(+ (messages[0].content as unknown as { text: string }[])[0].text,+ "Server copy",+ );+});diff --git a/studio/frontend/tests/saved-history-reconciliation.test.ts b/studio/frontend/tests/saved-history-reconciliation.test.tsnew file mode 100644index 00000000000..7606f310ee3--- /dev/null+++ b/studio/frontend/tests/saved-history-reconciliation.test.ts@@ -0,0 +1,200 @@+// 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 { readFileSync } from "node:fs";+import test from "node:test";+import { fileURLToPath } from "node:url";++import ts from "typescript";++import type { MessageStatus } from "@assistant-ui/react";++import { registerBundlerResolver } from "./helpers/kit.ts";++registerBundlerResolver();++const {+ beginSavedHistoryReconciliation,+ isSavedHistoryReconciliationSuperseded,+ reconcileOrdinarySavedMessagesInExport,+ reconcileOrdinarySavedMessagesInView,+ shouldReconcileOrdinaryStoredAssistant,+} = await import(+ "../src/features/chat/utils/saved-history-reconciliation.ts"+);+const { subscribeGenerationRecoveryTriggers } = await import(+ "../src/features/chat/utils/chat-generation-recovery.ts"+);++const assistantRuntime = (id: string, text: string) =>+ ({+ id,+ role: "assistant" as const,+ createdAt: new Date(0),+ content: [{ type: "text" as const, text }],+ status: { type: "complete", reason: "unknown" } as MessageStatus,+ metadata: { custom: {}, steps: [], unstable_annotations: [], unstable_data: [], unstable_state: null },+ });++const storedAssistant = (id: string, text: string) => ({+ id,+ threadId: "thread-1",+ parentId: "user-1",+ role: "assistant" as const,+ content: [{ type: "text" as const, text }],+ createdAt: 0,+});++test("ordinary saved assistant text reconciles into the open export", () => {+ const exported = {+ headId: "assistant-1",+ messages: [+ {+ parentId: "user-1",+ message: assistantRuntime("assistant-1", "Old checkpoint"),+ },+ ],+ };+ const { messages, changed } = reconcileOrdinarySavedMessagesInExport(+ exported,+ [storedAssistant("assistant-1", "New saved checkpoint")],+ { editingMessageId: null },+ );+ assert.equal(changed, true);+ const text = (messages[0].message.content as unknown as { text: string }[])[0].text;+ assert.equal(text, "New saved checkpoint");+});++test("a running assistant message is not reconciled", () => {+ const runtime = assistantRuntime("assistant-1", "Old checkpoint");+ runtime.status = { type: "running" };+ assert.equal(+ shouldReconcileOrdinaryStoredAssistant({+ stored: storedAssistant("assistant-1", "New saved checkpoint"),+ runtime,+ editingMessageId: null,+ }),+ false,+ );+});++test("the message being edited is not reconciled", () => {+ assert.equal(+ shouldReconcileOrdinaryStoredAssistant({+ stored: storedAssistant("assistant-1", "New saved checkpoint"),+ runtime: assistantRuntime("assistant-1", "Old checkpoint"),+ editingMessageId: "assistant-1",+ }),+ false,+ );+});++test("a superseded reconciliation generation is dropped", () => {+ const threadId = "thread-1";+ const first = beginSavedHistoryReconciliation(threadId);+ const second = beginSavedHistoryReconciliation(threadId);+ assert.equal(isSavedHistoryReconciliationSuperseded(threadId, first), true);+ assert.equal(isSavedHistoryReconciliationSuperseded(threadId, second), false);+});++test("reconcileOrdinarySavedMessagesInView imports without writing storage", () => {+ let imported = false;+ const view = {+ threadListItem: () => ({+ getState: () => ({ remoteId: "thread-1" }),+ }),+ thread: () => ({+ export: () => ({+ headId: "assistant-1",+ messages: [+ {+ parentId: "user-1",+ message: assistantRuntime("assistant-1", "Old checkpoint"),+ },+ ],+ }),+ import: () => {+ imported = true;+ },+ }),+ };+ const changed = reconcileOrdinarySavedMessagesInView(+ view,+ "thread-1",+ [storedAssistant("assistant-1", "New saved checkpoint")],+ { editingMessageId: null },+ );+ assert.equal(changed, true);+ assert.equal(imported, true);+});++test("window focus triggers recovery when the document is visible", () => {+ const windowTarget = new EventTarget();+ const documentTarget = Object.assign(new EventTarget(), {+ visibilityState: "visible",+ });+ let recoveries = 0;+ const unsubscribe = subscribeGenerationRecoveryTriggers(+ windowTarget,+ documentTarget,+ () => {+ recoveries += 1;+ },+ );+ windowTarget.dispatchEvent(new Event("focus"));+ unsubscribe();+ assert.equal(recoveries, 1);+});++// The wake handler is the only caller; deleting the call or its guard leaves every test above green.+test("recoverCurrentThread reconciles ordinary saved messages", () => {+ const source = fileURLToPath(+ new URL("../src/features/chat/runtime-provider.tsx", import.meta.url),+ );+ const parsed = ts.createSourceFile(+ "runtime-provider.tsx",+ readFileSync(source, "utf8"),+ ts.ScriptTarget.Latest,+ true,+ ts.ScriptKind.TSX,+ );+ let handler: ts.Node | null = null;+ const walk = (node: ts.Node): void => {+ if (+ ts.isVariableDeclaration(node) &&+ ts.isIdentifier(node.name) &&+ node.name.text === "recoverCurrentThread"+ ) {+ handler = node;+ return;+ }+ ts.forEachChild(node, walk);+ };+ walk(parsed);+ assert.ok(handler, "recoverCurrentThread not found");+ const calls = new Set<string>();+ let reconcileGuarded = false;+ const collect = (node: ts.Node): void => {+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {+ calls.add(node.expression.text);+ if (node.expression.text === "reconcileOrdinarySavedMessagesInView") {+ for (let p = node.parent; p; p = p.parent) {+ if (ts.isIfStatement(p) && p.expression.getText() === "fromBackend") {+ reconcileGuarded = true;+ }+ }+ }+ }+ ts.forEachChild(node, collect);+ };+ collect(handler);+ for (const name of [+ "beginSavedHistoryReconciliation",+ "isSavedHistoryReconciliationSuperseded",+ "reconcileOrdinarySavedMessagesInView",+ ]) {+ assert.ok(calls.has(name), `${name} not called from recoverCurrentThread`);+ }+ assert.ok(reconcileGuarded, "reconciliation must skip legacy fallback reads");+});