Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/web/store/__tests__/liftExcludesDraftLeak.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
7 changes: 5 additions & 2 deletions packages/web/store/chatSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>, incoming: Array<{ _id: string }>) => {
let lifted: Record<string, unknown> | 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.
Expand Down
Loading