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(bots): a later same-text send never claims an earlier send's message

Chrisd9cc477efc3d
diff --git a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
index 92d17a6071..5ea7e4f10d 100644
--- a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
+++ b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
@@ -396,9 +396,9 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {
() => (state.kind === 'ready' ? projectBotGroupExecutionFailures(mergeBotGroupMessages(state.older, state.group.messages), state.group.executionFailures) : []),
[state],
);
- const pendingMatches = useMemo(() => matchBotGroupPendingSends(pendingSends, realMessages), [pendingSends, realMessages]);
/** Real message id → the key its optimistic copy had, so the row is kept, not remounted. */
const adoptedKeysRef = useRef(new Map<string, string>());
+ const pendingMatches = useMemo(() => matchBotGroupPendingSends(pendingSends, realMessages, adoptedKeysRef.current), [pendingSends, realMessages]);
for (const [clientId, messageId] of pendingMatches) adoptedKeysRef.current.set(messageId, pendingBotGroupMessageId(clientId));
useEffect(() => {
if (pendingMatches.size === 0) return;
diff --git a/apps/desktop/src/renderer/features/bots/BotGroupTimelineModel.ts b/apps/desktop/src/renderer/features/bots/BotGroupTimelineModel.ts
index f84a5a1d90..0e9c02ffa6 100644
--- a/apps/desktop/src/renderer/features/bots/BotGroupTimelineModel.ts
+++ b/apps/desktop/src/renderer/features/bots/BotGroupTimelineModel.ts
@@ -181,11 +181,14 @@ const NO_MATCHES: ReadonlyMap<string, string> = new Map();
/**
* Which pending sends have shown up, as clientId → message id. A send main accepted matches its
* message id; before the answer it matches the first own message after it with the same text and
- * as many attachments (each message settles one send).
+ * as many attachments (each message settles one send). `adopted` (real message id → the pending
+ * row key that already took it) carries those settlements across renders, so a later send of the
+ * same words never claims a message an earlier send already became.
*/
export function matchBotGroupPendingSends(
pending: readonly BotGroupPendingSend[],
displayed: readonly BotGroupMessageView[],
+ adopted: ReadonlyMap<string, string> = NO_MATCHES,
): ReadonlyMap<string, string> {
if (pending.length === 0) return NO_MATCHES;
const ids = new Set(displayed.map((message) => message.id));
@@ -200,7 +203,8 @@ export function matchBotGroupPendingSends(
for (const send of pending) {
if (send.messageId) continue;
const text = send.text.trim();
- const echo = displayed.find((message) => !claimed.has(message.id) && isOwnMessage(message)
+ const key = pendingBotGroupMessageId(send.clientId);
+ const echo = displayed.find((message) => !claimed.has(message.id) && (adopted.get(message.id) ?? key) === key && isOwnMessage(message)
&& message.sequence > send.afterSequence && message.content.trim() === text
&& message.attachments.length === send.attachments.length);
if (!echo) continue;
diff --git a/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx b/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
index 8cbab5d38d..97d6cf2576 100644
--- a/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
+++ b/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
@@ -162,8 +162,8 @@ export function ChatThreadPanel({ group, rootId, quoteSources = group.messages,
const separator = t('bots.groupChat.memberSeparator');
const realReplies = useMemo(() => projectBotGroupExecutionFailures(replies, executionFailures), [replies, executionFailures]);
- const pendingMatches = useMemo(() => matchBotGroupPendingSends(pendingSends, realReplies), [pendingSends, realReplies]);
const adoptedKeysRef = useRef(new Map<string, string>());
+ const pendingMatches = useMemo(() => matchBotGroupPendingSends(pendingSends, realReplies, adoptedKeysRef.current), [pendingSends, realReplies]);
for (const [clientId, messageId] of pendingMatches) adoptedKeysRef.current.set(messageId, pendingBotGroupMessageId(clientId));
useEffect(() => {
if (pendingMatches.size === 0) return;
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupTimelineModel.test.ts b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupTimelineModel.test.ts
index cc15cfe7c8..77d14381c4 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupTimelineModel.test.ts
+++ b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupTimelineModel.test.ts
@@ -12,6 +12,7 @@ import {
matchBotGroupPendingSends,
mergeBotGroupLatestPage,
pendingBotGroupMessage,
+ pendingBotGroupMessageId,
settleBotGroupPendingSends,
stabilizeById,
type BotGroupLoadedTimeline,
@@ -103,6 +104,16 @@ describe('optimistic sends', () => {
expect(settleBotGroupPendingSends(pending, displayed)).toEqual([]);
});
+ it('never lets a later send of the same words claim the message an earlier send already became', () => {
+ // 'a' settled into srv-a and left the pending list; 'b' (same words) is still on its way.
+ const adopted = new Map([['srv-a', pendingBotGroupMessageId('a')]]);
+ const displayed = [own('srv-a', 3, '好')];
+ expect(matchBotGroupPendingSends([send('b', '好')], displayed, adopted).size).toBe(0);
+ // Its own echo still settles it, and so does a re-render while 'a' itself is still pending.
+ expect([...matchBotGroupPendingSends([send('b', '好')], [...displayed, own('srv-b', 4, '好')], adopted)]).toEqual([['b', 'srv-b']]);
+ expect([...matchBotGroupPendingSends([send('a', '好')], displayed, adopted)]).toEqual([['a', 'srv-a']]);
+ });
+
it('keeps a send whose message has not arrived, and the same array when nothing settled', () => {
const pending = [send('a', '好')];
expect(settleBotGroupPendingSends(pending, [own('old', 1, '好')])).toBe(pending);