Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(chat): keep per-message reaction versions bounded per group

Dash0d342ddbf220
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
index e83c8731b2..d31004fff9 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
@@ -41,7 +41,7 @@ vi.mock('ws', async () => {
close() { this.emit('close'); }
} };
});
-import { withChatServer } from '../chatServer.js';
+import { REACTION_REVISION_MESSAGE_LIMIT, withChatServer } from '../chatServer.js';
import { projectBotGroupExecutionFailures } from '@cindy/maker-shared/botGroupPresentation';
import { botGroupRuntimeFailureCode, settleUndispatchedBotGroupTurn } from '../botGroupRuntimeFailure.js';
import { authorizeGroupTool } from '../botGroupToolAuthorization.js';
@@ -1121,6 +1121,18 @@ describe('Chat Server result delivery and refresh', () => {
socket.emit('message', JSON.stringify({ type: 'scope_error', scope: `conversation:${roomId}`, error: { code: 'RESET_REQUIRED' } }));
await vi.advanceTimersByTimeAsync(0);
expect(await revisions()).toEqual({ room: 1, messages: {} });
+ // Per-message versions stay bounded: one more message past the limit re-versions the whole room instead.
+ const reacted = (ids: string[], cursor: string) => socket.emit('message', JSON.stringify({ type: 'changes', scope: `conversation:${roomId}`, cursor,
+ changes: ids.map(id => ({ type: 'reaction.changed', entityId: id, data: { emoji: '👍', present: true } })) }));
+ const ids = Array.from({ length: REACTION_REVISION_MESSAGE_LIMIT }, (_, i) => `m${i}`);
+ reacted(ids, '7');
+ const state = await revisions();
+ expect(state?.room).toBe(1);
+ expect(Object.keys(state?.messages ?? {})).toHaveLength(REACTION_REVISION_MESSAGE_LIMIT);
+ reacted(['m0'], '8');
+ expect((await revisions())?.messages?.m0).toBe(2);
+ reacted(['one-more'], '9');
+ expect(await revisions()).toEqual({ room: 2, messages: {} });
});
it('refreshes a reset room and resumes realtime from the authorized snapshot cursor', async () => {
await service.getGroup(roomId);
diff --git a/apps/desktop/src/main/maker-ipc/chatServer.ts b/apps/desktop/src/main/maker-ipc/chatServer.ts
index cf639ce024..fe471aa0b0 100644
--- a/apps/desktop/src/main/maker-ipc/chatServer.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServer.ts
@@ -87,6 +87,8 @@ const CHAT_AUTH_RETRY_MS = 60_000;
/** Main-group messages kept as background for a Thread request (the Thread itself is read separately). */
const THREAD_BACKGROUND_LIMIT = 30;
const ATTACHMENT_REFRESH_COALESCE_MS = 200;
+/** Messages per group with their own reaction version; past this the group re-versions as a whole, like a reset. */
+export const REACTION_REVISION_MESSAGE_LIMIT = 500;
const CHAT_REFRESHABLE_ERROR_CODES = new Set(['TOKEN_EXPIRED', 'INVALID_TOKEN', 'AUTH_REQUIRED']);
const recoverableAuth = (error: ChatResponseError) => error.status === 401
&& (CHAT_REFRESHABLE_ERROR_CODES.has(error.message) || error.message === 'INVALID_CHAT_RESPONSE');
@@ -185,12 +187,14 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
// message; a reset that skipped events versions the whole room (and makes per-message versions moot).
// Both ride on the group, so every loaded page and the Thread see them on refresh.
const reactionRevisions = new Map<string, { room: number; messages: Record<string, number> }>();
+ const resetReactions = (roomId: string) => reactionRevisions.set(roomId, { room: (reactionRevisions.get(roomId)?.room ?? 0) + 1, messages: {} });
+ // A long-open group stays bounded: once too many messages carry their own version, the group re-versions instead.
const bumpMessageReactions = (roomId: string, messageId: string) => {
const revisions = reactionRevisions.get(roomId) ?? { room: 0, messages: {} };
+ if (!(messageId in revisions.messages) && Object.keys(revisions.messages).length >= REACTION_REVISION_MESSAGE_LIMIT) return resetReactions(roomId);
revisions.messages[messageId] = (revisions.messages[messageId] ?? 0) + 1;
reactionRevisions.set(roomId, revisions);
};
- const resetReactions = (roomId: string) => reactionRevisions.set(roomId, { room: (reactionRevisions.get(roomId)?.room ?? 0) + 1, messages: {} });
let profiles: Array<typeof botProfiles.$inferSelect> = [];
const workspaces = () => chatServerWorkspaces(config.baseUrl, selfId, current);
const planning = new Map<string, { botId: string; controller: AbortController }>();
diff --git a/docs/product-rules/bot-group-chat.md b/docs/product-rules/bot-group-chat.md
index cec68fed73..17f1332fe6 100644
--- a/docs/product-rules/bot-group-chat.md
+++ b/docs/product-rules/bot-group-chat.md
@@ -367,6 +367,7 @@ Chat Server 既有的回应者接口直到最后一页(离群成员保留回
读取只读,不触发伙伴资料同步等任何写入。加载期间按人数预留头像位,不撑动消息;读取失败或名单与人数暂不一致时
如实显示数字。回应数、自己的状态或这条消息的实时回应事件(含人数不变、只换了人的情况)变化时重新读取;
回应版本按消息记在群详情上,已加载的更早消息页与话题里的徽标也会随之更新,别的消息被回应不会让它重新读取;
+单个群记版本的消息超过 500 条时,改为整群加一次版本并清空记录,避免长时间打开时无限增长;
实时连接断开期间,只换人不换数的变化要到重连回放后才更新,无法回放而重置时,整个群的回应都按已变化重新读取。回应者是否在群、叫什么,
始终按最新的群成员资料双向投影:改昵称、离群或重新加入都会立即更新,离群后沿用读取时的名字并标注不在群中。点击仍是添加/取消回应。回归见 `ChatReactionButton.test.tsx`
与 `chatServer.test.ts`;手机端暂不显示回应。