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: load the attachment hasher before deleting a chat attachment (#15751)

Daniel Han90a6543ffeec
diff --git a/studio/frontend/src/features/chat/index.ts b/studio/frontend/src/features/chat/index.ts
index bcf3578f616..61171505c41 100644
--- a/studio/frontend/src/features/chat/index.ts
+++ b/studio/frontend/src/features/chat/index.ts
@@ -485,7 +485,10 @@ export {
onChatThreadsDeleted,
removeChatThreadTombstones,
} from "./utils/chat-thread-tombstones";
-export { emitChatAttachmentDeleted } from "./utils/chat-attachment-events";
+export {
+ emitChatAttachmentDeleted,
+ prepareChatAttachmentDeletion,
+} from "./utils/chat-attachment-events";
export { forkCountFor, subscribeForkCounts } from "./utils/fork-count-store";
export { resolveReasoningGroupDuration } from "./utils/reasoning-duration";
export {
diff --git a/studio/frontend/src/features/chat/utils/chat-attachment-events.ts b/studio/frontend/src/features/chat/utils/chat-attachment-events.ts
index 9632146c5c2..edee9b7dfa2 100644
--- a/studio/frontend/src/features/chat/utils/chat-attachment-events.ts
+++ b/studio/frontend/src/features/chat/utils/chat-attachment-events.ts
@@ -93,6 +93,14 @@ export async function chatContentPartAttachmentIdFromSignature(
return `${CONTENT_PART_ID_PREFIX}${hex}`;
}
+/** Call before deleting: loads the hasher an open chat needs to drop this id, so a failed chunk load aborts the delete. */
+export async function prepareChatAttachmentDeletion(
+ attachmentId: string,
+): Promise<void> {
+ if (!attachmentId.startsWith(CONTENT_PART_ID_PREFIX)) return;
+ await chatContentPartAttachmentIdFromSignature("");
+}
+
type Listener = (event: ChatAttachmentDeletedEvent) => void | Promise<void>;
const listeners = new Set<Listener>();
diff --git a/studio/frontend/src/features/library/store.ts b/studio/frontend/src/features/library/store.ts
index 63d8b0e8aab..6d72e45d38f 100644
--- a/studio/frontend/src/features/library/store.ts
+++ b/studio/frontend/src/features/library/store.ts
@@ -3,7 +3,11 @@
import { create } from "zustand";
import { AUTH_SESSION_CLEARED_EVENT, getAuthSessionEpoch } from "@/features/auth";
-import { deleteFineTunedModel, emitChatAttachmentDeleted } from "@/features/chat";
+import {
+ deleteFineTunedModel,
+ emitChatAttachmentDeleted,
+ prepareChatAttachmentDeletion,
+} from "@/features/chat";
import { usePinnedModelsStore } from "@/features/model-picker";
import { translate } from "@/i18n";
import { type GalleryKind, notifyGalleryChanged } from "@/lib/gallery-flags";
@@ -197,6 +201,9 @@ export const useLibraryStore = create<LibraryState>((set, get) => {
usePinnedModelsStore.getState().unpinRepo(model.path);
return;
}
+ if (id.startsWith("attachment:")) {
+ await prepareChatAttachmentDeletion(id.split(":").slice(2).join(":"));
+ }
await deleteLibraryItem(id, fingerprint);
} finally {
star.settle();
diff --git a/studio/frontend/src/features/settings/components/uploaded-files-dialog.tsx b/studio/frontend/src/features/settings/components/uploaded-files-dialog.tsx
index f542611416a..cd4005487d3 100644
--- a/studio/frontend/src/features/settings/components/uploaded-files-dialog.tsx
+++ b/studio/frontend/src/features/settings/components/uploaded-files-dialog.tsx
@@ -19,6 +19,7 @@ import {
fetchChatAttachmentBlob,
isTextAttachmentName,
listChatAttachments,
+ prepareChatAttachmentDeletion,
} from "@/features/chat";
import {
type UploadedDocument,
@@ -315,6 +316,7 @@ function chatAttachmentRow(att: ChatAttachmentRecord): UploadedFileRow {
});
},
remove: async () => {
+ await prepareChatAttachmentDeletion(att.id);
await deleteChatAttachment(att.messageId, att.id);
// Patch any loaded runtime copy so a later sync cannot write the attachment back.
emitChatAttachmentDeleted({
diff --git a/studio/frontend/tests/chat-attachment-delete-preflight.test.ts b/studio/frontend/tests/chat-attachment-delete-preflight.test.ts
new file mode 100644
index 00000000000..71f672f8eb8
--- /dev/null
+++ b/studio/frontend/tests/chat-attachment-delete-preflight.test.ts
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// Without WebCrypto (plain http on a LAN address) the lazy node-forge chunk must load before a deletion commits.
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import { readSrc } from "./helpers/kit.ts";
+import { loadWithStubs } from "./helpers/module-stubs.ts";
+
+type Events = {
+ prepareChatAttachmentDeletion: (attachmentId: string) => Promise<void>;
+};
+
+const HASHED_ID = "content-part-sha256-ab";
+
+function loadEvents(forge: "ok" | "fails", loads: string[]): Events {
+ const fakeForge = {
+ md: {
+ sha256: {
+ create: () => ({
+ update: () => {},
+ digest: () => ({ toHex: () => "00" }),
+ }),
+ },
+ },
+ };
+ const stubs = {
+ get "node-forge"() {
+ loads.push("node-forge");
+ if (forge === "fails")
+ throw new Error("Failed to fetch dynamically imported module");
+ return { default: fakeForge };
+ },
+ };
+ return loadWithStubs<Events>(
+ new URL(
+ "../src/features/chat/utils/chat-attachment-events.ts",
+ import.meta.url,
+ ),
+ stubs,
+ );
+}
+
+async function withoutSubtle(run: () => Promise<void>): Promise<void> {
+ const descriptor = Object.getOwnPropertyDescriptor(globalThis, "crypto");
+ Object.defineProperty(globalThis, "crypto", {
+ value: {},
+ configurable: true,
+ writable: true,
+ });
+ try {
+ await run();
+ } finally {
+ if (descriptor) {
+ Object.defineProperty(globalThis, "crypto", descriptor);
+ } else {
+ Reflect.deleteProperty(globalThis, "crypto");
+ }
+ }
+}
+
+test("with WebCrypto, preparing a deletion does not load node-forge", async () => {
+ const loads: string[] = [];
+ const events = loadEvents("fails", loads);
+ await events.prepareChatAttachmentDeletion(HASHED_ID);
+ assert.deepEqual(loads, []);
+});
+
+test("without WebCrypto, a node-forge chunk that cannot load fails the deletion up front", async () => {
+ await withoutSubtle(async () => {
+ const loads: string[] = [];
+ const events = loadEvents("fails", loads);
+ await assert.rejects(events.prepareChatAttachmentDeletion(HASHED_ID));
+ assert.deepEqual(loads, ["node-forge"]);
+ });
+});
+
+test("without WebCrypto, a loadable node-forge lets the deletion go ahead", async () => {
+ await withoutSubtle(async () => {
+ const loads: string[] = [];
+ const events = loadEvents("ok", loads);
+ await events.prepareChatAttachmentDeletion(HASHED_ID);
+ assert.deepEqual(loads, ["node-forge"]);
+ });
+});
+
+test("ids the open chat matches without hashing need nothing loaded", async () => {
+ await withoutSubtle(async () => {
+ const loads: string[] = [];
+ const events = loadEvents("fails", loads);
+ await events.prepareChatAttachmentDeletion("content-part-3");
+ await events.prepareChatAttachmentDeletion("3f2c9b1e-attachment");
+ assert.deepEqual(loads, []);
+ });
+});
+
+test("the uploaded files dialog prepares before it deletes", () => {
+ const source = readSrc(
+ "features/settings/components/uploaded-files-dialog.tsx",
+ );
+ const remove = source.slice(source.indexOf("remove: async () => {"));
+ const prepared = remove.indexOf(
+ "await prepareChatAttachmentDeletion(att.id)",
+ );
+ const deleted = remove.indexOf("await deleteChatAttachment(");
+ assert.ok(prepared >= 0, "remove() should prepare the deletion");
+ assert.ok(deleted > prepared, "remove() should prepare before deleting");
+});
diff --git a/studio/frontend/tests/library-state.test.ts b/studio/frontend/tests/library-state.test.ts
index 900d4c2fcf6..3950eadcb04 100644
--- a/studio/frontend/tests/library-state.test.ts
+++ b/studio/frontend/tests/library-state.test.ts
@@ -187,6 +187,7 @@ function loadStore(
emitted: unknown[] = [],
session: { epoch: number } = { epoch: 0 },
starsUndone: string[] = [],
+ prepareChatAttachmentDeletion: (attachmentId: string) => Promise<void> = async () => {},
) {
return loadWithStubs<{
useLibraryStore: zustand.UseBoundStore<
@@ -207,6 +208,7 @@ function loadStore(
"@/features/chat": {
deleteFineTunedModel: async () => {},
emitChatAttachmentDeleted: (event: unknown) => emitted.push(event),
+ prepareChatAttachmentDeletion,
},
"@/features/model-picker": {
usePinnedModelsStore: {
@@ -384,6 +386,42 @@ test("deleting a chat attachment tells an open chat to drop it", async () => {
assert.deepEqual(emitted, [{ messageId: "m:1", attachmentId: "content-part-x" }]);
});
+test("a chat attachment is not deleted when an open chat could not drop it afterwards", async () => {
+ const emitted: unknown[] = [];
+ const deleted: string[] = [];
+ const prepared: string[] = [];
+ let chunkLoads = false;
+ const store = loadStore(
+ {
+ getLibrary: async () => ({
+ items: [item("attachment:m%3A1:content-part-sha256-ab")],
+ folders: [],
+ }),
+ deleteLibraryItem: async (id: string) => {
+ deleted.push(id);
+ },
+ },
+ emitted,
+ { epoch: 0 },
+ [],
+ async (attachmentId) => {
+ prepared.push(attachmentId);
+ if (!chunkLoads) throw new Error("Failed to fetch dynamically imported module");
+ },
+ );
+ await store.getState().refresh();
+ await assert.rejects(store.getState().removeItem("attachment:m%3A1:content-part-sha256-ab"));
+ assert.deepEqual(deleted, []);
+ assert.deepEqual(emitted, []);
+
+ chunkLoads = true;
+ await store.getState().removeItem("attachment:m%3A1:content-part-sha256-ab");
+ await store.getState().removeItem("upload:a");
+ assert.deepEqual(prepared, ["content-part-sha256-ab", "content-part-sha256-ab"]);
+ assert.deepEqual(deleted, ["attachment:m%3A1:content-part-sha256-ab", "upload:a"]);
+ assert.deepEqual(emitted, [{ messageId: "m:1", attachmentId: "content-part-sha256-ab" }]);
+});
+
function loadDownloads(
fflate: object,
saved: (File | Blob)[],