Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(chat): fence untracked snapshot reads across reconnects
Chris72c332925b22
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.tsindex c1fb217105..6b0b6fa0c3 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts@@ -2071,7 +2071,13 @@ describe('Chat Server result delivery and refresh', () => {expect(pushes()).toBe(1);});- it.each([false, true])('uses current members after reconnect before list sync or replay finishes (late snapshot: %s)', async (lateSnapshot) => {+ it.each([+ ['cached room', false, false],+ ['late read of subscribed room', true, false],+ ['late read of unsubscribed room', true, true],+ ] as const)('uses current members after reconnect before list sync or replay finishes (%s)', async (_label, lateSnapshot, untracked) => {+ const targetRoom = untracked ? '10000000-0000-4000-8000-000000000002' : roomId;+ const targetSnapshot = () => ({ body: { ...snapshotOf().body, room: room(targetRoom) } });const base = fixture.handle.getMockImplementation()!;let holdList = false;let finishList!: () => void;@@ -2082,20 +2088,21 @@ describe('Chat Server result delivery and refresh', () => {if (route === '/me') return { body: { actor: { id: selfId }, capabilities: { conversationListStateVersion: 1 } } };if (route.startsWith('/conversation-list-state?')) return holdList? new Promise(resolve => { finishList = () => resolve({ body: [] }); }) : { body: [] };- if (route.endsWith('/snapshot')) {+ if (route === `/conversations/${targetRoom}/snapshot`) {if (holdSnapshot) {holdSnapshot = false;- return new Promise(resolve => { finishSnapshot = () => resolve(snapshotOf()); });+ return new Promise(resolve => { finishSnapshot = () => resolve(targetSnapshot()); });}- if (newMembers) return { body: { ...snapshotOf().body,+ if (newMembers) return { body: { ...targetSnapshot().body,members: [selfMember, { ...botMember, id: peerId, name: 'New teammate' }] } };}return base(route, method, data);});const previous = await live();+ if (untracked) expect(vi.mocked(previous.send).mock.calls.some(([raw]) => String(raw).includes(targetRoom))).toBe(false);previous.emit('close');holdSnapshot = lateSnapshot;- const oldRead = lateSnapshot ? service.getGroup(roomId) : undefined;+ const oldRead = lateSnapshot ? service.getGroup(targetRoom) : undefined;await vi.advanceTimersByTimeAsync(500);holdList = true;newMembers = true;@@ -2105,9 +2112,10 @@ describe('Chat Server result delivery and refresh', () => {if (lateSnapshot) { finishSnapshot(); await oldRead; }deps.decidePlan = vi.fn(() => new Promise(() => {})) as typeof deps.decidePlan;const from = fixture.handle.mock.calls.length;- expect(await send('reconnect-members')).toMatchObject({ ok: true });+ expect(await service.sendMessage({ groupId: targetRoom, text: 'Make a draft', clientId: 'reconnect-members',+ mentions: { all: false, botIds: [] } })).toMatchObject({ ok: true });await vi.advanceTimersByTimeAsync(0);- expect(routesSince(from)).toContain(`GET /conversations/${roomId}/snapshot`);+ expect(routesSince(from)).toContain(`GET /conversations/${targetRoom}/snapshot`);expect(deps.decidePlan).toHaveBeenCalledWith(expect.objectContaining({organizerName: 'New teammate', members: [{ botId: peerId, name: 'New teammate', description: '' }],}), expect.any(AbortSignal));diff --git a/apps/desktop/src/main/maker-ipc/chatServer.ts b/apps/desktop/src/main/maker-ipc/chatServer.tsindex e5ade027b6..ef76c705ae 100644--- a/apps/desktop/src/main/maker-ipc/chatServer.ts+++ b/apps/desktop/src/main/maker-ipc/chatServer.ts@@ -421,6 +421,7 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD*/const generations = new Map<string, number>();const generation = (roomId: string) => generations.get(roomId) ?? 0;+ let snapshotConnection = 0;const snapshots = new Map<string, { value: Snapshot; generation: number; at: number }>();const summaries = new Map<string, { key: string; value: BotGroupDetail; at: number }>();const invalidate = (roomId: string) => {@@ -812,8 +813,10 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceDmemberJoinedClaims = true;reconnectDelay = 500; connected = true;threads.reset();- // Subscription replay and list sync may lag behind ready. Retire cached state- // now; advancing the generation also fences reads started before this connection.+ // Replay and list sync may lag behind ready. Fence all snapshot reads,+ // including rooms whose first read has not reached subscribe yet.+ snapshotConnection += 1;+ snapshots.clear();for (const roomId of rooms.keys()) invalidate(roomId);ws.send(JSON.stringify({ type: 'subscribe', scope: `actor:${selfId}`, after: '0' }));for (const [roomId, cursor] of rooms) ws.send(JSON.stringify({ type: 'subscribe', scope: `conversation:${roomId}`, after: cursor }));@@ -890,10 +893,12 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceDasync function readSnapshot(roomId: string) {id.parse(roomId); await ensureRegistered();const read = generation(roomId);+ const connection = snapshotConnection;const value = await api<Snapshot>(`/conversations/${roomId}/snapshot`);if (value.room.dissolved_at) retirePlanning(roomId);- // Only an answer no change notice has overtaken may be reused.- if (current() && generation(roomId) === read) snapshots.set(roomId, { value, generation: read, at: Date.now() });+ // A prior connection or room change makes this answer ineligible for reuse.+ if (current() && snapshotConnection === connection && generation(roomId) === read)+ snapshots.set(roomId, { value, generation: read, at: Date.now() });return value;}async function snapshot(roomId: string) {