Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(chat): preserve reply recipients when retrying lost acknowledgements
Chrisfd59d5e036d0
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.tsindex 1360e80c9c..fe7b54d637 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts@@ -394,6 +394,47 @@ describe('Chat Server result delivery and refresh', () => {expect(posts()).toHaveLength(0);});+ // Server operationId replay compares the whole submitted payload before returning its receipt.+ // A lost ACK followed by a membership change must not turn a committed reply into a conflict.+ it.each(['quote', 'thread'] as const)('replays the committed %s recipients after the companion leaves', async entry => {+ let committed: unknown;+ fixture.handle.mockImplementation((route, method, body) => {+ if (route.endsWith('/members') && committed) return { body: [{ ...botMember, state: 'left' }] };+ if (route.endsWith('/messages') && method === 'POST') {+ if (!committed) {+ committed = structuredClone(body);+ throw Object.assign(new Error('socket hang up'), { code: 'ECONNRESET' });+ }+ if (JSON.stringify(body) !== JSON.stringify(committed)) return { status: 409, body: { error: { code: 'OPERATION_CONFLICT' } } };+ }+ return quoteResponse(quoted())(route, method);+ });+ const input = { groupId: roomId, text: 'Continue', clientId: `lost-ack-${entry}`, mentions: { all: false, botIds: [] } };+ const send = () => entry === 'thread'+ ? service.chatServer!.reply({ ...input, rootId: quotedId })+ : service.sendMessage({ ...input, quote: { messageId: quotedId } });+ expect(await send()).toMatchObject({ ok: false });+ expect(await send()).toEqual({ ok: true, messageId: 'saved-quote' });+ expect(posts()).toHaveLength(2);+ expect(posts()[1]![2].mentions).toEqual([botId]);+ });++ it('resolves Thread recipients again after a definite refusal stored no message', async () => {+ let refused = false;+ fixture.handle.mockImplementation((route, method) => {+ if (route.endsWith('/members') && refused) return { body: [{ ...botMember, state: 'left' }] };+ if (route.endsWith('/messages') && method === 'POST' && !refused) {+ refused = true;+ return { status: 400, body: { error: { code: 'MENTION_NOT_MEMBER' } } };+ }+ return quoteResponse(quoted())(route, method);+ });+ const input = { groupId: roomId, rootId: quotedId, text: 'Continue', clientId: 'thread-refused', mentions: { all: false, botIds: [] } };+ expect(await service.chatServer!.reply(input)).toMatchObject({ ok: false, errorCode: 'MENTION_NOT_MEMBER' });+ expect(await service.chatServer!.reply(input)).toEqual({ ok: true, messageId: 'saved-quote' });+ expect(posts()[1]![2].mentions).toEqual([]);+ });+it('keeps a lost post\'s quote when a retry then fails before posting with a 4xx', async () => {let original: unknown = quoted();let stage: 'lost' | 'refuse-upload' | 'ok' = 'lost';diff --git a/apps/desktop/src/main/maker-ipc/chatServer.ts b/apps/desktop/src/main/maker-ipc/chatServer.tsindex 60d385316a..9aa557986f 100644--- a/apps/desktop/src/main/maker-ipc/chatServer.ts+++ b/apps/desktop/src/main/maker-ipc/chatServer.ts@@ -1851,6 +1851,9 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceDreturn parsed.data;}const operationId = z.string().min(8).max(160).regex(/^[a-zA-Z0-9_.:-]+$/);+ // Keep the recipients once a Thread POST may have committed, including when the+ // controller loses our successful answer. The server still authorizes every replay.+ const postedThreadRecipients = new Map<string, string[]>();const chatServer: ChatServerApi = {ownedBots: () => result(async () => { await register(); return { bots: actors.flatMap(a => { const bot = a.kind === 'bot' ? localBot(a.id) : undefined; return bot?.status === 'active' ? [{ actorId: a.id, botId: bot.id, name: a.name }] : []; }) }; }),refreshProfile: () => result(async () => {@@ -1946,9 +1949,20 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceDconst i = z.object({ groupId: id, rootId: id, text: z.string().trim().min(1).max(8000), clientId: operationId,mentions: z.object({ all: z.boolean(), botIds: z.array(z.string()).max(100) }) }).parse(input);i.groupId = await groupRoom(i.groupId);- const posted = await api<{ id: string }>(`/conversations/${i.groupId}/messages`, 'POST', {- operationId: i.clientId, threadRootId: i.rootId, content: [{ type: 'text', text: i.text }], mentions: await mentionIds(i.groupId, i.mentions, i.rootId),- });+ // Include the caller's intent: editing a failed send must not reuse old recipients.+ const key = JSON.stringify(i);+ const recipients = postedThreadRecipients.get(key) ?? await mentionIds(i.groupId, i.mentions, i.rootId);+ postedThreadRecipients.set(key, recipients);+ if (postedThreadRecipients.size > 128) postedThreadRecipients.delete(postedThreadRecipients.keys().next().value!);+ let posted: { id: string };+ try {+ posted = await api<{ id: string }>(`/conversations/${i.groupId}/messages`, 'POST', {+ operationId: i.clientId, threadRootId: i.rootId, content: [{ type: 'text', text: i.text }], mentions: recipients,+ });+ } catch (error) {+ if (error instanceof ChatResponseError && error.status >= 400 && error.status < 500 && error.status !== 408) postedThreadRecipients.delete(key);+ throw error;+ }changed(i.groupId); return { messageId: posted.id };}),react: input => result(async () => {@@ -2014,10 +2028,11 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD* Quote snapshots of sends that have not finished: from just before the POST (attachments* already uploaded) until the send* returns ok (post-send bookkeeping included) or the server definitely refuses the POST.- * Invariant: every retry of one clientId posts the same content, even if the original has been- * deleted since, so the server returns the message it already stored instead of a second one.+ * Invariant: retries keep the quote and recipients even if the original is deleted or its+ * author leaves, so the server returns the stored message instead of an operation conflict.*/- const postedQuotes = new Map<string, BotGroupQuoteView & { recipientActorId?: string }>();+ const postedQuotes = new Map<string, { quote: BotGroupQuoteView & { recipientActorId?: string };+ mentions: { all: boolean; botIds: string[] }; recipients: string[] }>();/*** A sidebar summary. The room list carries each room's change head, which every message,* execution, plan and membership change advances; a room whose head, revision and local state@@ -2049,7 +2064,7 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceDconst snapshotRead = recentSnapshot(i.groupId, true);// An unopened room must be confirmed as a group before reading its other resources.if (!rooms.has(i.groupId)) await snapshotRead;- const quoteKey = i.quote ? `${i.groupId}:${i.clientId}:${i.quote.messageId}` : '';+ const quoteKey = i.quote ? JSON.stringify([i.groupId, i.clientId, i.quote.messageId, i.text, i.mentions, i.division]) : '';const knownQuote = i.quote ? postedQuotes.get(quoteKey) : undefined;const snapshotMembers = snapshotRead.then(value => value.members);const quoteRead = i.quote && !knownQuote ? quoteSnapshot(i.groupId, i.quote.messageId, snapshotMembers) : undefined;@@ -2058,9 +2073,9 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceDfor (const read of [snapshotMembers, quoteRead, plansRead, membersRead]) read?.catch(() => undefined);const s = await snapshotRead;// Read before preparing attachments: an unavailable quote must not stage files.- const quote = i.quote ? knownQuote ?? await quoteRead! : null;+ const quote = i.quote ? knownQuote?.quote ?? await quoteRead! : null;const members = membersRead ? await membersRead : [];- i.mentions = withReplyRecipient(members, i.mentions, quote?.recipientActorId);+ i.mentions = knownQuote?.mentions ?? withReplyRecipient(members, i.mentions, quote?.recipientActorId);const plans = await plansRead;const openPlan = plans.find(p => ['proposed','running','waiting'].includes(p.status));const proposed = openPlan?.status === 'proposed' && openPlan.creator_id === selfId;@@ -2078,11 +2093,11 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceDlet result: { id: string };let posting = false;try {- const mentions = membersRead ? resolveMentions(members, i.mentions) : [];+ const mentions = knownQuote?.recipients ?? (membersRead ? resolveMentions(members, i.mentions) : []);const uploaded = await media.upload(i.groupId, i.clientId, prepared?.ok ? prepared.attachments : [], selfId);// Cached only once the message may reach the server; a failure before this stored nothing.if (quote) {- postedQuotes.set(quoteKey, quote);+ postedQuotes.set(quoteKey, { quote, mentions: i.mentions, recipients: mentions });if (postedQuotes.size > 128) postedQuotes.delete(postedQuotes.keys().next().value!);}posting = true;