Open source CapCut alternative.
TypeScript
fix(cut): keep chat history in IndexedDB, say when a save fails, and drop a deleted project's threads
David Lefbd01f3ea213
diff --git a/site/src/cut/components/AiPanel.tsx b/site/src/cut/components/AiPanel.tsxindex 8ae51d20..537b68cc 100644--- a/site/src/cut/components/AiPanel.tsx+++ b/site/src/cut/components/AiPanel.tsx@@ -59,9 +59,15 @@ import {chatThreadDeleted,deleteStoredThread,isStoredThread,+ loadProjectThreads,+ queueKey,readActiveChat,readRawThreads,recordThread,+ seenRepliesKey,+ subscribeThreadSaveFailure,+ threadSaveFailure,+ threadsLoaded,writeActiveChat,writeRawThreads,subscribeChatThreads,@@ -187,7 +193,6 @@ interface ChatThread {const THREAD_LIMIT = 30;-const seenRepliesKey = (projectId: string) => `cut-ai-seen-replies-${projectId}`;// Which reply each thread has been read up to, per project: a small store// over localStorage, so the panel reads it as a snapshot and any writer —@@ -408,8 +413,6 @@ const FAVS_KEY = "cut-ai-favs";// Waiting composer-queue rows, saved per thread so a reload or a project// switch brings them back. Running and done rows stay out — their turns die// with the page.-const queueKey = (projectId: string, threadId: string) =>- `cut-ai-queue-${projectId}-${threadId}`;function readStoredQueue(projectId: string,threadId: string,@@ -581,20 +584,29 @@ export function AiPanel({useEffect(() => {onStatusChange(projectId, chatStatus);}, [projectId, chatStatus, onStatusChange]);- // The owner's chat opens from this browser's storage at once; the cloud- // copy merges in behind it and a session reloads its thread when the merge- // writes it. A shared viewer has no thread of their own, so their view- // waits for the owner's threads to land before it can pick one.- const [chatsReady, setChatsReady] = useState(!readOnly);+ // The owner's chat opens once this browser's stored history has loaded+ // (IndexedDB, so a beat after mount — opening before it would show an empty+ // thread and save it over the real one); the cloud copy merges in behind it+ // and a session reloads its thread when the merge writes it. A shared viewer+ // has no thread of their own, so their view waits for the owner's threads+ // to land before it can pick one.+ const [chatsReady, setChatsReady] = useState(() => !readOnly && threadsLoaded(projectId));useEffect(() => {let alive = true;- void ensureCloudThreads(projectId).then(() => {- if (alive) setChatsReady(true);- });+ void loadProjectThreads(projectId)+ .then(() => {+ if (!alive) return;+ setThreads(readThreads(projectId));+ if (!readOnly) setChatsReady(true);+ return ensureCloudThreads(projectId);+ })+ .then(() => {+ if (alive) setChatsReady(true);+ });return () => {alive = false;};- }, [projectId]);+ }, [projectId, readOnly]);useEffect(() => {let stopped = false;@@ -1058,6 +1070,8 @@ function ChatSession({// A transient warning in the folder tab above the composer — the same slot// the credits tab uses — for refusals like the frame capacity below.const [notice, setNotice] = useState<string | null>(null);+ // History that can't be saved is lost on reload, so say so while it lasts.+ const saveFailure = useSyncExternalStore(subscribeThreadSaveFailure, threadSaveFailure, () => null);const noticeTimer = useRef<ReturnType<typeof setTimeout> | null>(null);const showNotice = (msg: string) => {setNotice(msg);@@ -2204,6 +2218,17 @@ function ChatSession({<span>{notice}</span></div>)}+ {!outOfCredits && !notice && saveFailure && (+ <div+ className={cn(+ "ai-save-failure-tab pointer-events-auto mx-1.5 flex items-center gap-1.5 rounded-t-lg border border-b-0 border-amber-500/30 bg-amber-50 px-3 pt-1.5 pb-2.5 text-[11px] text-amber-800",+ trayVisible && "-mb-1",+ )}+ >+ <TriangleAlert className="size-3 shrink-0" />+ <span>{saveFailure}</span>+ </div>+ )}{trayVisible && (<div className="pointer-events-auto"><ComposerQueuediff --git a/site/src/cut/lib/cache.ts b/site/src/cut/lib/cache.tsindex 9957c215..51621a0d 100644--- a/site/src/cut/lib/cache.ts+++ b/site/src/cut/lib/cache.ts@@ -17,10 +17,13 @@import { currentEngineUser } from "./api";const DB_NAME = "cut";-// v1 held only the per-project view state; v2 adds this store beside it.-const DB_VERSION = 2;+// v1 held only the per-project view state; v2 adds this store beside it; v3+// adds the chat history (chatThreads.ts), which outgrew localStorage.+const DB_VERSION = 3;const UI_STORE = "project-ui";const SNAPSHOT_STORE = "snapshots";+/** Each project's chat thread list, keyed by project id. */+export const CHAT_STORE = "chat-threads";/** A snapshot older than this is dropped on read — too old to be a useful* head start, and old enough that painting it would be a lie. */@@ -39,8 +42,17 @@ export function openCutDb(): Promise<IDBDatabase> {const names = req.result.objectStoreNames;if (!names.contains(UI_STORE)) req.result.createObjectStore(UI_STORE);if (!names.contains(SNAPSHOT_STORE)) req.result.createObjectStore(SNAPSHOT_STORE);+ if (!names.contains(CHAT_STORE)) req.result.createObjectStore(CHAT_STORE);+ };+ req.onsuccess = () => {+ // A newer tab upgrading the database waits on every open connection;+ // step aside so it can, and reopen at the next read.+ req.result.onversionchange = () => {+ req.result.close();+ db = null;+ };+ resolve(req.result);};- req.onsuccess = () => resolve(req.result);req.onerror = () => reject(req.error);}).then((conn) => {diff --git a/site/src/cut/lib/chatCloud.ts b/site/src/cut/lib/chatCloud.tsindex 5f4573d3..2b6c9076 100644--- a/site/src/cut/lib/chatCloud.ts+++ b/site/src/cut/lib/chatCloud.ts@@ -1,5 +1,5 @@// Cloud mirror of a project's chat threads. Local and browser projects keep-// chat purely in localStorage; a cloud project's threads also sync to the hosted+// chat purely in this browser (chatThreads.ts); a cloud project's threads also sync to the hosted// /projects/:id/chats routes so the history follows the account across// devices. Threads stay opaque here — the module reads only id and updatedAt// to merge, and ships the same slimmed payload the panel writes to storage.@@ -9,6 +9,7 @@ import {chatThreadDeleted,deleteStoredThread,isStoredThread,+ loadProjectThreads,mergeThreads,readProjectThreads,writeProjectThreads,@@ -62,7 +63,9 @@ export async function putCloudThread(/** Merge changed conversations and retry local saves, including offline deletions. */export function ensureCloudThreads(projectId: string, refresh = false): Promise<void> {const backend = getBackend();- if (backend.kind === "local" || backend.kind === "browser") return Promise.resolve();+ // The engine and browser stores keep no copy of their own: the history this+ // browser holds is all there is, once it has loaded.+ if (backend.kind === "local" || backend.kind === "browser") return loadProjectThreads(projectId);if (!syncState || syncState.projectId !== projectId || syncState.backend !== backend)syncState = { projectId, backend, versions: new Map(), seeded: false, dirty: true };const state = syncState;@@ -76,6 +79,8 @@ export function ensureCloudThreads(projectId: string, refresh = false): Promise<async function syncThreads(state: NonNullable<typeof syncState>): Promise<void> {const { projectId, backend, versions } = state;+ // Merge into what this browser stored, never into a not-yet-read empty list.+ await loadProjectThreads(projectId);const path = `/api/cut/projects/${projectId}/chats`;const response = await backend.fetch(`${path}?revisions=1`);if (!response.ok) throw new Error("Could not read the project's chats.");@@ -172,7 +177,7 @@ async function flushOne(key: string, keepalive = false): Promise<void> {syncState.versions.delete(entry.threadId);syncState.dirty = true;}- // The thread is still in localStorage; the next sync pushes it up.+ // The thread is still stored in this browser; the next sync pushes it up.}}diff --git a/site/src/cut/lib/chatThreads.test.ts b/site/src/cut/lib/chatThreads.test.tsindex 59b3b49a..fdaf1659 100644--- a/site/src/cut/lib/chatThreads.test.ts+++ b/site/src/cut/lib/chatThreads.test.ts@@ -1,4 +1,4 @@-import { beforeEach, describe, expect, test } from "bun:test";+import { beforeEach, describe, expect, spyOn, test } from "bun:test";import { Chat } from "@ai-sdk/react";import type { ChatTransport, UIMessage, UIMessageChunk } from "ai";@@ -6,13 +6,23 @@ import {chatThreadDeleted,deleteStoredThread,clearProjectThreads,+ flushThreadSaves,+ loadProjectThreads,mergeThreads,+ readActiveChat,readProjectThreads,readThreadIds,recordThread,+ subscribeThreadSaveFailure,+ sweepGoneProjectThreads,+ threadSaveFailure,threadsKey,+ threadsLoaded,+ useThreadStore,+ writeActiveChat,writeProjectThreads,writeRawThreads,+ type ThreadStore,} from "./chatThreads";// Bun's test runtime has no localStorage; the module reads it per call.@@ -23,9 +33,38 @@ beforeEach(() => {getItem: (k: string) => store.get(k) ?? null,setItem: (k: string, v: string) => void store.set(k, v),removeItem: (k: string) => void store.delete(k),+ get length() { return store.size; },+ key: (i: number) => [...store.keys()][i] ?? null,};+ // With no IndexedDB here the threads fall back to localStorage, as a+ // browser without one does.+ useThreadStore(null);});+/** IndexedDB as the thread store sees it: one list per project, and a quota+ * that refuses a write past it the way the browser does. */+function memoryStore(quota = Infinity, pressure?: number) {+ const lists = new Map<string, unknown[]>();+ const size = () => [...lists.values()].reduce((n, list) => n + JSON.stringify(list).length, 0);+ const threadStore: ThreadStore = {+ read: async (projectId) => lists.get(projectId),+ write: async (projectId, list) => {+ const before = lists.get(projectId);+ lists.set(projectId, list);+ if (size() > quota) {+ if (before) lists.set(projectId, before); else lists.delete(projectId);+ throw new DOMException("The quota has been exceeded.", "QuotaExceededError");+ }+ },+ remove: async (projectId) => { lists.delete(projectId); },+ projects: async () => [...lists.keys()],+ pressure: async () => pressure,+ };+ return { threadStore, lists, size };+}++const big = (id: string, updatedAt: number, chars: number) => ({ id, updatedAt, messages: [{ id: "m", role: "user", parts: [{ type: "text", text: "x".repeat(chars) }] }] });+describe("stored chat threads", () => {test("a project's threads round trip", () => {writeProjectThreads("p1", [{ id: "a", updatedAt: 2 }, { id: "b", updatedAt: 1 }]);@@ -153,3 +192,127 @@ describe("a running thread's record", () => {recorder.stop();});});++describe("where threads are kept", () => {+ test("history goes to the thread store, past what localStorage could hold", async () => {+ const { threadStore, lists } = memoryStore();+ useThreadStore(threadStore);+ await loadProjectThreads("p");+ // Six megabytes: more than a whole origin's localStorage.+ writeProjectThreads("p", [big("a", 1, 3_000_000), big("b", 2, 3_000_000)]);+ await flushThreadSaves();+ expect((lists.get("p") as { id: string }[]).map((t) => t.id)).toEqual(["a", "b"]);+ expect(store.has(threadsKey("p"))).toBe(false);+ expect(threadSaveFailure()).toBeNull();+ });++ test("localStorage history moves to the store on first read, freeing the space", async () => {+ const { threadStore, lists } = memoryStore();+ store.set(threadsKey("p1"), JSON.stringify([{ id: "a", updatedAt: 1 }]));+ store.set(threadsKey("gone"), JSON.stringify([{ id: "b", updatedAt: 2 }]));+ store.set("cut-ai-model", "claude");+ useThreadStore(threadStore);+ expect(threadsLoaded("p1")).toBe(false);+ await loadProjectThreads("p1");+ expect(threadsLoaded("p1")).toBe(true);+ expect(readProjectThreads("p1").map((t) => t.id)).toEqual(["a"]);+ // Every project's history moves, not just the one opened.+ expect([...lists.keys()].sort()).toEqual(["gone", "p1"]);+ expect([...store.keys()]).toEqual(["cut-ai-model"]);+ });++ test("a store that already holds a project keeps the newest copy of each thread", async () => {+ const { threadStore, lists } = memoryStore();+ lists.set("p", [{ id: "a", updatedAt: 5, title: "store" }]);+ store.set(threadsKey("p"), JSON.stringify([{ id: "a", updatedAt: 3, title: "local" }, { id: "b", updatedAt: 4 }]));+ useThreadStore(threadStore);+ await loadProjectThreads("p");+ expect(readProjectThreads("p")).toEqual([{ id: "a", updatedAt: 5, title: "store" } as never, { id: "b", updatedAt: 4 }]);+ });++ test("a deleted project takes its threads, open chat, queue and read marks with it", async () => {+ const { threadStore, lists } = memoryStore();+ useThreadStore(threadStore);+ await loadProjectThreads("p");+ writeProjectThreads("p", [{ id: "a", updatedAt: 1 }]);+ writeActiveChat("p", "a");+ store.set("cut-ai-queue-p-a", "[]");+ store.set("cut-ai-seen-replies-p", "{}");+ store.set("cut-ai-queue-other-a", "[]");+ await flushThreadSaves();+ clearProjectThreads("p");+ await flushThreadSaves();+ expect(lists.has("p")).toBe(false);+ expect(readProjectThreads("p")).toEqual([]);+ expect(readActiveChat("p")).toBeNull();+ expect([...store.keys()]).toEqual(["cut-ai-queue-other-a"]);+ });++ test("threads of projects the listings no longer hold go once the project is confirmed gone", async () => {+ const { threadStore, lists } = memoryStore();+ lists.set("kept", [{ id: "a" }]);+ lists.set("elsewhere", [{ id: "b" }]);+ lists.set("deleted", [{ id: "c" }]);+ useThreadStore(threadStore);+ const asked: string[] = [];+ await sweepGoneProjectThreads(new Set(["kept"]), async (id) => { asked.push(id); return id === "deleted"; });+ expect(asked.sort()).toEqual(["deleted", "elsewhere"]);+ expect([...lists.keys()].sort()).toEqual(["elsewhere", "kept"]);+ // Each project is asked about once per page.+ await sweepGoneProjectThreads(new Set(["kept"]), async (id) => { asked.push(id); return false; });+ expect(asked).toHaveLength(2);+ });++ test("a full store makes room from other projects' oldest threads first", async () => {+ const { threadStore, lists } = memoryStore(1_000_000);+ lists.set("old", [big("o1", 1, 300_000), big("o2", 5, 300_000)]);+ lists.set("older", [big("o0", 0, 300_000)]);+ useThreadStore(threadStore);+ await loadProjectThreads("p");+ writeProjectThreads("p", [big("mine", 9, 200_000)]);+ await flushThreadSaves();+ expect(threadSaveFailure()).toBeNull();+ expect((lists.get("p") as { id: string }[]).map((t) => t.id)).toEqual(["mine"]);+ // The oldest thread went; the newer one of another project stayed.+ expect(lists.has("older")).toBe(false);+ expect((lists.get("old") as { id: string }[]).map((t) => t.id)).toEqual(["o1", "o2"]);+ });++ test("a store near its quota prunes before it writes", async () => {+ const { threadStore, lists } = memoryStore(Infinity, 0.95);+ lists.set("other", [big("o1", 1, 1000), big("o2", 2, 1000)]);+ useThreadStore(threadStore);+ await loadProjectThreads("p");+ writeProjectThreads("p", [big("mine", 9, 500)]);+ await flushThreadSaves();+ expect((lists.get("other") as { id: string }[]).map((t) => t.id)).toEqual(["o2"]);+ expect(lists.has("p")).toBe(true);+ });++ test("a save that still fails is reported once, and clears when a save lands", async () => {+ const { threadStore } = memoryStore(1000);+ useThreadStore(threadStore);+ const warn = spyOn(console, "warn").mockImplementation(() => {});+ let changes = 0;+ const stop = subscribeThreadSaveFailure(() => { changes++; });+ try {+ await loadProjectThreads("p");+ writeProjectThreads("p", [big("a", 1, 5000)]);+ await flushThreadSaves();+ writeProjectThreads("p", [big("a", 2, 6000)]);+ await flushThreadSaves();+ expect(threadSaveFailure()).toContain("couldn't be saved");+ expect(warn.mock.calls).toHaveLength(1);+ expect(changes).toBe(1);+ // The live copy is still there for the page to show.+ expect(readProjectThreads("p")[0].updatedAt).toBe(2);+ writeProjectThreads("p", [{ id: "a", updatedAt: 3 }]);+ await flushThreadSaves();+ expect(threadSaveFailure()).toBeNull();+ expect(changes).toBe(2);+ } finally {+ stop();+ warn.mockRestore();+ }+ });+});diff --git a/site/src/cut/lib/chatThreads.ts b/site/src/cut/lib/chatThreads.tsindex 40e41f0d..5afd569b 100644--- a/site/src/cut/lib/chatThreads.ts+++ b/site/src/cut/lib/chatThreads.ts@@ -10,19 +10,27 @@export const threadsKey = (projectId: string) => `cut-ai-threads-${projectId}`;// The open chat survives hiding the panel — only the + button starts a new one.export const activeChatKey = (projectId: string) => `cut-ai-active-${projectId}`;+// Messages typed while a reply streams wait here, per thread.+export const queueKey = (projectId: string, threadId: string) => `cut-ai-queue-${projectId}-${threadId}`;+// Which background replies the user has seen, per project.+export const seenRepliesKey = (projectId: string) => `cut-ai-seen-replies-${projectId}`;const changes = new Set<(projectId: string) => void>();+const notify = (projectId: string) => { for (const onChange of changes) onChange(projectId); };export function subscribeChatThreads(projectId: string, onChange: () => void): () => void {const local = (id: string) => { if (id === projectId) onChange(); };+ // Without a thread store, other tabs' writes arrive as storage events; with+ // one, over the broadcast channel (which lands in `changes`).const remote = (event: StorageEvent) => {if (event.key === threadsKey(projectId)) onChange();};changes.add(local);+ listenAcrossTabs();window.addEventListener("storage", remote);return () => { changes.delete(local); window.removeEventListener("storage", remote); };}-// Where the threads live. An owner's are durable in localStorage; a shared+// Where the threads live. An owner's are durable in this browser; a shared// viewer holds the owner's copy in memory for the tab, so reading someone// else's project leaves nothing in this browser — and an owner opening their// own share link keeps their stored history untouched.@@ -41,10 +49,84 @@ export type StoredThread = { id: string; updatedAt?: number; deleted?: boolean }export const isStoredThread = (v: unknown): v is StoredThread =>!!v && typeof v === "object" && typeof (v as StoredThread).id === "string";-/** A project's thread list exactly as written. The AI panel owns the payload;- * this module owns where it lives. */-export function readRawThreads(projectId: string): unknown[] {- if (memory) return memory.threads.get(projectId) ?? [];+/** Durable thread lists, one per project. IndexedDB in the browser: a long+ * agent turn's history runs to megabytes, and localStorage's 5 MB per origin+ * — shared by every project and every other key — filled up and silently+ * dropped every save after. */+export interface ThreadStore {+ read(projectId: string): Promise<unknown[] | undefined>;+ /** Rejects when the browser refuses the write (QuotaExceededError when full). */+ write(projectId: string, list: unknown[]): Promise<void>;+ remove(projectId: string): Promise<void>;+ projects(): Promise<string[]>;+ /** How full the origin's storage is, 0–1, when the browser says. */+ pressure(): Promise<number | undefined>;+}++async function chatStore<T>(mode: IDBTransactionMode, op: (store: IDBObjectStore) => IDBRequest<T>): Promise<T> {+ // Loaded on first use, so this module stays a leaf the generate store and+ // the server-free tests can import.+ const { openCutDb, CHAT_STORE } = await import("./cache");+ const conn = await openCutDb();+ return new Promise<T>((resolve, reject) => {+ const tx = conn.transaction(CHAT_STORE, mode);+ const req = op(tx.objectStore(CHAT_STORE));+ tx.oncomplete = () => resolve(req.result);+ tx.onerror = tx.onabort = () => reject(tx.error ?? req.error ?? new Error("chat store transaction failed"));+ });+}++const indexedDbStore: ThreadStore = {+ read: (projectId) => chatStore("readonly", (s) => s.get(projectId)).then((v) => (Array.isArray(v) ? v : undefined)),+ write: (projectId, list) => chatStore("readwrite", (s) => s.put(list, projectId)).then(() => undefined),+ remove: (projectId) => chatStore("readwrite", (s) => s.delete(projectId)).then(() => undefined),+ projects: () => chatStore("readonly", (s) => s.getAllKeys()).then((keys) => keys.map(String)),+ pressure: async () => {+ const estimate = await navigator.storage?.estimate?.().catch(() => undefined);+ return estimate?.quota ? (estimate.usage ?? 0) / estimate.quota : undefined;+ },+};++// The thread store in use: undefined until first needed, null when there is+// none and history falls back to localStorage (no IndexedDB, or it failed).+let backing: ThreadStore | null | undefined;+function threadStore(): ThreadStore | null {+ if (backing === undefined) backing = typeof indexedDB === "undefined" ? null : indexedDbStore;+ return backing;+}++// Each loaded project's list, so the panel reads synchronously; writes land+// here first and reach the store in the background.+const cache = new Map<string, unknown[]>();+const loads = new Map<string, Promise<void>>();+const loaded = new Set<string>();+const dirty = new Set<string>();+let migration: Promise<void> | null = null;+let queue: Promise<void> = Promise.resolve();+let flushScheduled = false;+const persistedSize = new Map<string, number>();+// How long a load waits on the store (another tab can hold the database on an+// older version) before this tab falls back to localStorage.+const LOAD_TIMEOUT_MS = 5000;++/** Swap the thread store — tests, and the fallback when IndexedDB fails. Null+ * means localStorage. Resets everything held in memory. */+export function useThreadStore(store: ThreadStore | null): void {+ backing = store;+ cache.clear();+ loads.clear();+ loaded.clear();+ dirty.clear();+ persistedSize.clear();+ migration = null;+ queue = Promise.resolve();+ flushScheduled = false;+ goneChecked.clear();+ setSaveFailure(null);+}++const legacyPrefix = threadsKey("");+function readLegacy(projectId: string): unknown[] {try {const v = JSON.parse(localStorage.getItem(threadsKey(projectId)) ?? "[]") as unknown;return Array.isArray(v) ? v : [];@@ -53,6 +135,92 @@ export function readRawThreads(projectId: string): unknown[] {}}+const merged = (...lists: unknown[][]) => mergeThreads(...lists.map((l) => l.filter(isStoredThread)));+const sizeOf = (list: unknown[]) => JSON.stringify(list).length;++/** Move every project's localStorage history into the store, once per page,+ * then free the keys. A key whose write fails stays put for the next try. */+function migrateLegacyThreads(store: ThreadStore): Promise<void> {+ migration ??= (async () => {+ let keys: string[] = [];+ try {+ for (let i = 0; i < localStorage.length; i++) {+ const key = localStorage.key(i);+ if (key?.startsWith(legacyPrefix)) keys.push(key);+ }+ } catch {+ keys = [];+ }+ for (const key of keys) {+ const projectId = key.slice(legacyPrefix.length);+ const legacy = readLegacy(projectId);+ try {+ const stored = (await store.read(projectId)) ?? [];+ await store.write(projectId, merged(stored, legacy));+ // A page that fell back to localStorage meanwhile still reads the key.+ if (backing === store) localStorage.removeItem(key);+ } catch (err) {+ console.warn(`[chat] couldn't move project ${projectId}'s chat history out of localStorage`, err);+ }+ }+ })();+ return migration;+}++function withTimeout<T>(work: Promise<T>, ms: number): Promise<T> {+ return new Promise<T>((resolve, reject) => {+ const timer = setTimeout(() => reject(new Error(`chat store didn't answer in ${ms}ms`)), ms);+ work.then((v) => { clearTimeout(timer); resolve(v); }, (err) => { clearTimeout(timer); reject(err); });+ });+}++/** Read a project's history from the store into memory, once. The panel waits+ * on this before showing threads, so it never flashes an empty history. */+export function loadProjectThreads(projectId: string): Promise<void> {+ const store = threadStore();+ if (memory || !store || loaded.has(projectId)) return Promise.resolve();+ let load = loads.get(projectId);+ if (!load) {+ load = withTimeout(+ (async () => {+ await migrateLegacyThreads(store);+ const stored = (await store.read(projectId)) ?? [];+ if (backing !== store) return;+ persistedSize.set(projectId, sizeOf(stored));+ // Anything written before the load finished joins what was stored, as+ // does a localStorage copy the migration couldn't move.+ const early = [...readLegacy(projectId), ...(cache.get(projectId) ?? [])];+ cache.set(projectId, early.length ? merged(stored, early) : stored);+ loaded.add(projectId);+ if (early.length) schedule(projectId);+ notify(projectId);+ })(),+ LOAD_TIMEOUT_MS,+ ).catch((err) => {+ if (backing !== store) return;+ console.warn("[chat] chat history store unavailable; using localStorage for this page", err);+ const early = [...cache];+ useThreadStore(null);+ for (const [id, list] of early) writeRawThreads(id, merged(readLegacy(id), list));+ });+ loads.set(projectId, load);+ }+ return load;+}++/** Whether a project's history has been read, so an empty list means none. */+export function threadsLoaded(projectId: string): boolean {+ return !!memory || !threadStore() || loaded.has(projectId);+}++/** A project's thread list exactly as written. The AI panel owns the payload;+ * this module owns where it lives. */+export function readRawThreads(projectId: string): unknown[] {+ if (memory) return memory.threads.get(projectId) ?? [];+ // Before its load lands, a project shows whatever localStorage still holds.+ return cache.get(projectId) ?? readLegacy(projectId);+}+/** Replace a project's thread list, payload untouched. */export function writeRawThreads(projectId: string, list: unknown[]): void {// Deletion wins over callbacks and remote copies still holding the old thread.@@ -60,14 +228,200 @@ export function writeRawThreads(projectId: string, list: unknown[]): void {if (deleted.length) list = mergeThreads(list.filter(isStoredThread), deleted);if (memory) {memory.threads.set(projectId, list);- for (const onChange of changes) onChange(projectId);+ notify(projectId);+ return;+ }+ if (!threadStore()) {+ try {+ localStorage.setItem(threadsKey(projectId), JSON.stringify(list));+ setSaveFailure(null);+ } catch (err) {+ saveFailed(err);+ }+ notify(projectId);return;}+ cache.set(projectId, list);+ notify(projectId);+ schedule(projectId);+}++/** Run store work in order: a removal never races a save of the same project. */+function enqueue(work: () => Promise<void>): Promise<void> {+ queue = queue.then(work, work);+ return queue;+}++function schedule(projectId: string): void {+ dirty.add(projectId);+ if (flushScheduled) return;+ flushScheduled = true;+ // A burst of writes (a streaming reply, a cloud merge) persists once.+ queueMicrotask(() => {+ flushScheduled = false;+ void enqueue(persistDirty);+ });+}++async function persistDirty(): Promise<void> {+ const store = backing;+ if (!store) return;+ for (const projectId of [...dirty]) {+ // A save waits for the project's load, which merges it with what was+ // stored rather than overwriting history this page never read.+ if (!loaded.has(projectId)) await loadProjectThreads(projectId);+ if (backing !== store || !dirty.has(projectId) || !loaded.has(projectId)) continue;+ const list = cache.get(projectId);+ if (!list) continue;+ dirty.delete(projectId);+ await persist(store, projectId, list);+ }+}++const isQuotaError = (err: unknown) =>+ !!err && typeof err === "object" && ((err as Error).name === "QuotaExceededError" || (err as { code?: number }).code === 22);++async function persist(store: ThreadStore, projectId: string, list: unknown[]): Promise<void> {+ const size = sizeOf(list);+ const growth = size - (persistedSize.get(projectId) ?? 0);try {- localStorage.setItem(threadsKey(projectId), JSON.stringify(list));- for (const onChange of changes) onChange(projectId);+ const pressure = await store.pressure().catch(() => undefined);+ if (pressure !== undefined && pressure > PRESSURE) await makeRoom(store, projectId, growth);+ for (let attempt = 0; ; attempt++) {+ try {+ await store.write(projectId, list);+ break;+ } catch (err) {+ // The browser doesn't say how far over it is: free what this save+ // adds, then twice that, and so on.+ if (!isQuotaError(err) || attempt >= 4) throw err;+ if ((await makeRoom(store, projectId, Math.max(growth, 1024) * 2 ** attempt)) === 0) throw err;+ }+ }+ persistedSize.set(projectId, size);+ removeLocal(threadsKey(projectId));+ setSaveFailure(null);+ broadcast(projectId);+ } catch (err) {+ saveFailed(err);+ }+}++/** Above this share of the origin's quota, a save first clears room. */+const PRESSURE = 0.9;++/** Free at least `bytes` by dropping the oldest threads of other projects —+ * the open project's history is the last thing to lose. Returns the bytes freed. */+async function makeRoom(store: ThreadStore, keep: string, bytes: number): Promise<number> {+ if (bytes <= 0) return 0;+ const lists = new Map<string, unknown[]>();+ for (const projectId of await store.projects()) {+ if (projectId === keep) continue;+ lists.set(projectId, (loaded.has(projectId) ? cache.get(projectId) : await store.read(projectId)) ?? []);+ }+ const candidates = [...lists].flatMap(([projectId, list]) =>+ list.filter(isStoredThread).filter((t) => !t.deleted).map((thread) => ({ projectId, thread })),+ );+ candidates.sort((a, b) => (a.thread.updatedAt ?? 0) - (b.thread.updatedAt ?? 0));+ let freed = 0;+ const dropped = new Map<string, Set<string>>();+ for (const { projectId, thread } of candidates) {+ if (freed >= bytes) break;+ freed += JSON.stringify(thread).length;+ if (!dropped.has(projectId)) dropped.set(projectId, new Set());+ dropped.get(projectId)!.add(thread.id);+ }+ for (const [projectId, ids] of dropped) {+ const rest = lists.get(projectId)!.filter((t) => !isStoredThread(t) || !ids.has(t.id));+ if (rest.some((t) => isStoredThread(t) && !t.deleted)) await store.write(projectId, rest);+ else await store.remove(projectId);+ persistedSize.set(projectId, sizeOf(rest));+ if (cache.has(projectId)) cache.set(projectId, rest);+ for (const id of ids) removeLocal(queueKey(projectId, id));+ notify(projectId);+ broadcast(projectId);+ }+ const count = [...dropped.values()].reduce((n, ids) => n + ids.size, 0);+ if (count) console.warn(`[chat] browser storage nearly full; dropped the ${count} oldest chat thread(s) of other projects`);+ return freed;+}++/** Wait until every write so far has reached the store. */+export async function flushThreadSaves(): Promise<void> {+ for (let i = 0; i < 10; i++) {+ await new Promise<void>((resolve) => queueMicrotask(resolve));+ await queue;+ await Promise.all(loads.values());+ if (!dirty.size || !backing) return;+ await enqueue(persistDirty);+ }+}++// A failed save is told once in the console and shown in the panel until a+// save lands again.+let saveFailure: string | null = null;+let warned = false;+const failureChanges = new Set<() => void>();+function setSaveFailure(message: string | null): void {+ if (message === saveFailure) return;+ saveFailure = message;+ for (const onChange of failureChanges) onChange();+}+function saveFailed(err: unknown): void {+ if (!warned) {+ warned = true;+ console.warn("[chat] couldn't save chat history in this browser", err);+ }+ setSaveFailure(+ isQuotaError(err)+ ? "Chat history couldn't be saved — this browser's storage is full."+ : "Chat history couldn't be saved in this browser.",+ );+}+export function threadSaveFailure(): string | null {+ return saveFailure;+}+export function subscribeThreadSaveFailure(onChange: () => void): () => void {+ failureChanges.add(onChange);+ return () => failureChanges.delete(onChange);+}++// Other tabs of this browser hear about saves to reread them.+let channel: BroadcastChannel | null | undefined;+function crossTab(): BroadcastChannel | null {+ if (channel === undefined) channel = typeof BroadcastChannel === "undefined" ? null : new BroadcastChannel("cut-ai-threads");+ return channel;+}+function broadcast(projectId: string): void {+ try {+ crossTab()?.postMessage({ projectId });+ } catch {+ // A closed channel only costs other tabs a stale list until reload.+ }+}+let listening = false;+function listenAcrossTabs(): void {+ const ch = crossTab();+ if (listening || !ch) return;+ listening = true;+ ch.addEventListener("message", (event: MessageEvent<{ projectId?: unknown }>) => {+ const projectId = event.data?.projectId;+ const store = backing;+ if (typeof projectId !== "string" || !store || !cache.has(projectId) || dirty.has(projectId)) return;+ void store.read(projectId).then((list) => {+ if (backing !== store || dirty.has(projectId)) return;+ cache.set(projectId, list ?? []);+ persistedSize.set(projectId, sizeOf(list ?? []));+ notify(projectId);+ }, () => {});+ });+}++function removeLocal(key: string): void {+ try {+ localStorage.removeItem(key);} catch {- // Storage full/blocked — history just won't persist.+ // Storage blocked — nothing to clear.}}@@ -121,20 +475,76 @@ export function readThreadIds(projectId: string): Set<string> {return new Set(readProjectThreads(projectId).filter((t) => !t.deleted).map((t) => t.id));}-/** Drop a project's chat history and active-thread pointer — called when the- * project itself is deleted, so no stale thread or its renders survive it. */+/** Drop everything a project's chat left in this browser — its threads, open+ * chat, queued messages and read marks — when the project itself is deleted,+ * so no stale thread or its renders survive it. */export function clearProjectThreads(projectId: string): void {if (memory) {memory.threads.delete(projectId);memory.active.delete(projectId);return;}+ const store = threadStore();+ if (store) {+ cache.set(projectId, []);+ loaded.add(projectId);+ dirty.delete(projectId);+ persistedSize.delete(projectId);+ void enqueue(async () => {+ try {+ await store.remove(projectId);+ broadcast(projectId);+ } catch {+ // The sweep of gone projects retries on a later visit.+ }+ });+ }+ const queued = queueKey(projectId, "");try {- localStorage.removeItem(threadsKey(projectId));- localStorage.removeItem(activeChatKey(projectId));+ const keys: string[] = [];+ for (let i = 0; i < localStorage.length; i++) {+ const key = localStorage.key(i);+ if (key?.startsWith(queued)) keys.push(key);+ }+ for (const key of keys) localStorage.removeItem(key);} catch {// Storage blocked — nothing to clear.}+ removeLocal(threadsKey(projectId));+ removeLocal(activeChatKey(projectId));+ removeLocal(seenRepliesKey(projectId));+ notify(projectId);+}++// Projects already asked about this page: a listing refetch doesn't re-ask.+const goneChecked = new Set<string>();++/** Clear the history of projects this browser holds threads for but no+ * listing shows, once `gone` confirms each is deleted (not merely on a+ * backend this page can't reach right now). */+export async function sweepGoneProjectThreads(listed: Set<string>, gone: (projectId: string) => Promise<boolean>): Promise<void> {+ if (memory) return;+ const store = threadStore();+ let held: string[] = [];+ try {+ if (store) {+ await migrateLegacyThreads(store);+ held = await store.projects();+ } else {+ for (let i = 0; i < localStorage.length; i++) {+ const key = localStorage.key(i);+ if (key?.startsWith(legacyPrefix)) held.push(key.slice(legacyPrefix.length));+ }+ }+ } catch {+ return;+ }+ for (const projectId of held) {+ if (listed.has(projectId) || goneChecked.has(projectId)) continue;+ goneChecked.add(projectId);+ if (await gone(projectId).catch(() => false)) clearProjectThreads(projectId);+ }+ await flushThreadSaves();}export function chatThreadDeleted(projectId: string, threadId: string): boolean {diff --git a/site/src/cut/lib/generate.ts b/site/src/cut/lib/generate.tsindex 2ce4a035..a2ba3eff 100644--- a/site/src/cut/lib/generate.ts+++ b/site/src/cut/lib/generate.ts@@ -8,7 +8,7 @@ import { cloudBackend } from "./backend/cloud";import { localBackend } from "./backend/local";import { normalizeRef, type AssetRef } from "./assetRef";import { bytesFromBase64 } from "./bytes";-import { readThreadIds } from "./chatThreads";+import { loadProjectThreads, readThreadIds, threadsLoaded } from "./chatThreads";import { composeGenPrompt, foldTextRefs } from "./composeGen";import {projectWriteMode,@@ -323,6 +323,8 @@ const JOBS_CAP = 50;* how a resumed caller (the scene pipeline) adopts a running job's result* instead of billing a second take. */const settlements = new Map<string, Promise<GenerateJob>>();+// Projects whose chat history the resume sweep has asked to load.+const threadLoads = new Set<string>();/** The settled-job promise for a running render, when this session owns it. */export function videoJobSettlement(jobId: string): Promise<GenerateJob> | undefined {@@ -898,6 +900,15 @@ export const useGenerate = create<GenerateState>((set, get) => {return ids;};for (const j of get().jobs) {+ // A project's history loads from IndexedDB; until it has, a missing+ // thread means unread, not deleted. Sweep again once it lands.+ if (j.chatId && !threadsLoaded(j.projectId)) {+ if (!threadLoads.has(j.projectId)) {+ threadLoads.add(j.projectId);+ void loadProjectThreads(j.projectId).then(() => get().resumeRunningJobs());+ }+ continue;+ }// A render whose owning thread no longer exists — the thread was deleted,// or its whole project was (which clears the project's threads) — must// never land: drop it so a reload can't resurrect media the user removed.diff --git a/site/src/cut/lib/projectCopy.test.ts b/site/src/cut/lib/projectCopy.test.tsindex 11e66ed6..f74130df 100644--- a/site/src/cut/lib/projectCopy.test.ts+++ b/site/src/cut/lib/projectCopy.test.ts@@ -16,6 +16,7 @@ await stubModule("./chatCloud", import.meta.url, {putCloudThread: async () => {},});await stubModule("./chatThreads", import.meta.url, {+ loadProjectThreads: async () => {},readProjectThreads: () => [],writeProjectThreads: () => {},mergeThreads: () => [],diff --git a/site/src/cut/lib/projectCopy.ts b/site/src/cut/lib/projectCopy.tsindex ef8d4a20..a9bc9c22 100644--- a/site/src/cut/lib/projectCopy.ts+++ b/site/src/cut/lib/projectCopy.ts@@ -6,7 +6,7 @@// the editor's move.import type { CutBackend } from "./backend/types";import { fetchCloudThreads, putCloudThread } from "./chatCloud";-import { mergeThreads, readProjectThreads, writeProjectThreads } from "./chatThreads";+import { loadProjectThreads, mergeThreads, readProjectThreads, writeProjectThreads } from "./chatThreads";import { ensureLinkedOnCloud, linkedIdsIn } from "./linkedLibrary";import { uploadProjectMediaTo } from "./media";import { localMediaFile } from "./mediaSync";@@ -25,6 +25,7 @@ async function copyChatThreads(srcProjectId: string,dstProjectId: string): Promise<void> {+ await loadProjectThreads(srcProjectId);const threads = mergeThreads(await fetchCloudThreads(src, srcProjectId),readProjectThreads(srcProjectId)diff --git a/site/src/cut/lib/queries.ts b/site/src/cut/lib/queries.tsindex f8b66b95..07fe0f80 100644--- a/site/src/cut/lib/queries.ts+++ b/site/src/cut/lib/queries.ts@@ -31,9 +31,11 @@ import {availableResidencies,backendFor,listedResidencies,+ projectGone,projectsSnapshotKey,type Residency,} from "./residency";+import { sweepGoneProjectThreads } from "./chatThreads";import type { ProjectFolder, ProjectSummary } from "./types";export { backendFor };@@ -72,6 +74,18 @@ function seedFromSnapshot<T>(client: QueryClient, queryKey: readonly unknown[],};}+// Each residency's latest listing this page. Once every live one has+// answered, chat history this browser holds for a project none of them lists+// is checked against its backends and dropped if the project is gone —+// deleted from another tab, another device, or with its folder.+const listings = new Map<Residency, Set<string>>();+function noteListing(r: Residency, data: ProjectsSection): void {+ listings.set(r, new Set(data.projects.map((p) => p.id)));+ if (!availableResidencies().every((x) => listings.has(x))) return;+ const listed = new Set([...listings.values()].flatMap((ids) => [...ids]));+ void sweepGoneProjectThreads(listed, projectGone);+}+async function fetchProjectsSection(r: Residency): Promise<ProjectsSection> {const backend = backendFor(r);const [res, fres] = await Promise.all([@@ -109,6 +123,7 @@ export function useProjectsSection(r: Residency, { list, live }: { list: booleantry {const data = await fetchProjectsSection(r);writeSnapshot(projectsSnapshotKey(r), data);+ noteListing(r, data);return data;} catch (err) {if (r === "local" && cutMode() === "local") {diff --git a/site/src/cut/lib/residency.ts b/site/src/cut/lib/residency.tsindex c526cfa3..b7c11bb2 100644--- a/site/src/cut/lib/residency.ts+++ b/site/src/cut/lib/residency.ts@@ -192,6 +192,33 @@ export async function resolveProjectPlacement(projectId: string): Promise<Projecreturn { mode: "cloud", reachable: true };}+/**+ * Whether a project is deleted everywhere it could live: the browser store+ * doesn't hold it, a reachable engine answers 404, and the cloud answers 404.+ * Anything that can't answer — an engine this browser has used but that isn't+ * running, a signed-out cloud, a network error — keeps it, so a closed app+ * never costs a project its chat history.+ */+export async function projectGone(projectId: string): Promise<boolean> {+ if (await browserHasProject(projectId).catch(() => true)) return false;+ if (servedFromEngine() || engineOrigin() !== "") {+ try {+ const res = await localBackend.fetch(`/api/cut/projects/${projectId}`, { method: "HEAD" });+ if (res.status !== 404) return false;+ } catch {+ return false;+ }+ } else if (engineSeen()) {+ return false;+ }+ try {+ const res = await cloudBackend.fetch(`/api/cut/projects/${projectId}`, { method: "HEAD" });+ return res.status === 404;+ } catch {+ return false;+ }+}+export async function resolveProjectMode(projectId: string): Promise<CutMode> {return (await resolveProjectPlacement(projectId)).mode;}