diff --git a/packages/web/store/__tests__/liftExcludesDraftLeak.test.ts b/packages/web/store/__tests__/liftExcludesDraftLeak.test.ts new file mode 100644 index 0000000000..137cdc2b45 --- /dev/null +++ b/packages/web/store/__tests__/liftExcludesDraftLeak.test.ts @@ -0,0 +1,41 @@ +import { beforeEach, describe, expect, it } from "bun:test"; +import { useInboxStore } from "../inboxStore"; + +// A push that carries a room this client tombstoned (a left room, a refused +// read) lifts the tombstone and lands the row (liftExcludesForIncoming). The +// lift rebuilt the collection by spreading the DRAFT, which copied a draft +// proxy for every other row into a frozen object finalize could not clean, so +// every sibling row reached committed state as a proxy revoked at commit: +// "Cannot perform 'get' on a proxy that has been revoked" on the next read. + +const serverId = (seed: string) => seed.padEnd(32, "0").slice(0, 32); +const ME = serverId("userme"); +const LEFT = serverId("chanleft"); +const KEPT = serverId("chankept"); +const channel = (id: string, name: string) => ({ _id: id, name, team_id: "t1", created_at: 1, updated_at: 1 }); + +describe("a push that lifts a tombstone", () => { + beforeEach(() => { + useInboxStore.setState({ + chatChannels: {}, + chatReads: {}, + chatRail: [], + pending: {}, + currentUser: { _id: ME }, + } as any); + }); + + it("leaves every other chat channel a plain, readable row", () => { + useInboxStore.getState().syncTable("chatChannels", [channel(LEFT, "left"), channel(KEPT, "kept")]); + useInboxStore.getState().retireChatChannel(LEFT); + expect(useInboxStore.getState().pending[`chatChannels:${LEFT}`]?.type).toBe("exclude"); + + useInboxStore.getState().syncTable("chatChannels", [channel(LEFT, "left again")]); + + const after = useInboxStore.getState(); + expect(after.pending[`chatChannels:${LEFT}`]).toBeUndefined(); + expect(after.chatChannels[LEFT]?.name).toBe("left again"); + expect(after.chatChannels[KEPT]?.name).toBe("kept"); + expect(() => structuredClone(after.chatChannels)).not.toThrow(); + }); +}); diff --git a/packages/web/store/chatSlice.ts b/packages/web/store/chatSlice.ts index 62e98f9f17..3cd8b704c2 100644 --- a/packages/web/store/chatSlice.ts +++ b/packages/web/store/chatSlice.ts @@ -1560,13 +1560,16 @@ export function createChatSlice(set: any, get: any): ChatSliceImpl { * row carried by a push is readable, so its tombstone is lifted and the row * lands. Runs after the merge, which skipped the row on the tombstone. */ export function liftExcludesForIncoming(collection: string) { - return (draft: any, _table: any, incoming: Array<{ _id: string }>) => { + return (draft: any, table: Record, incoming: Array<{ _id: string }>) => { let lifted: Record | null = null; for (const row of incoming) { const key = `${collection}:${row._id}`; if (draft.pending[key]?.type !== "exclude") continue; delete draft.pending[key]; - (lifted ??= { ...draft[collection] })[row._id] = row; + // Copy the plain table the sync just assigned, never the draft: spreading + // draft[collection] copies a child draft per row into a frozen object + // finalize cannot clean, and every row then commits as a revoked proxy. + (lifted ??= { ...table })[row._id] = row; } // A synced collection is a frozen whole (inboxStore.syncTable), so a lifted // row lands by replacing the object, never by writing into it.