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(bot-groups): 清除失效来源和不可贴回回复的旧重试状态

Dasheed70d611c26
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServerThreads.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServerThreads.test.ts
index 59a66e85cb..b2a1bad8a0 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServerThreads.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServerThreads.test.ts
@@ -303,6 +303,32 @@ describe('recovering from a failed read in a quiet room', () => {
expect(projection.shareSources(room, [share], []).get(sourceId)?.id).toBe(sourceId);
});
+ it.each(['source', 'preview'] as const)('invalidates failed %s reads immediately without waking another room early', async kind => {
+ const io = makeIo();
+ const projection = createThreadProjection(io);
+ const root = message({ id: rootA, replyCount: 1 });
+ const read = (id: string) => kind === 'source'
+ ? projection.shareSources(id, [share], []) : projection.previews(id, [root], fullWindow([]));
+ io.message.mockRejectedValue(new Error('offline'));
+ io.threadPage.mockRejectedValue(new Error('offline'));
+ read(room); read('other-room');
+ await projection.settled();
+ await vi.advanceTimersByTimeAsync(1_000);
+ projection.invalidate(room, sourceId);
+ io.message.mockResolvedValue(null); // The source was deleted.
+ io.threadPage.mockResolvedValue([reply(sourceId, rootA, { content: [{ type: 'text', text: 'edited' }] })]);
+ read(room); read('other-room');
+ await projection.settled();
+ const requests = kind === 'source' ? io.message.mock.calls : io.threadPage.mock.calls;
+ expect(requests.map(([id]) => id)).toEqual([room, 'other-room', room]);
+ if (kind === 'source') expect(read(room).get(sourceId)).toBeNull();
+ else expect(read(room).get(rootA)).toEqual([expect.objectContaining({ content: [{ type: 'text', text: 'edited' }] })]);
+ io.filled.mockClear();
+ await vi.advanceTimersByTimeAsync(29_000);
+ expect(io.filled.mock.calls).toEqual([['other-room']]);
+ projection.dispose();
+ });
+
it('backs off and stops retrying by itself after a bounded number of attempts', async () => {
const io = makeIo();
const projection = createThreadProjection(io);
diff --git a/apps/desktop/src/main/maker-ipc/chatServerThreads.ts b/apps/desktop/src/main/maker-ipc/chatServerThreads.ts
index 81a46bbc93..2ea97719bc 100644
--- a/apps/desktop/src/main/maker-ipc/chatServerThreads.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServerThreads.ts
@@ -91,7 +91,7 @@ export function createThreadProjection<M extends ThreadMessage>(io: {
armRetry(roomId);
}
function settledRetry(roomId: string, cacheKey: string) {
- failures.delete(cacheKey);
+ retryAt.delete(cacheKey); failures.delete(cacheKey);
if (pendingRetries.get(roomId)?.delete(cacheKey)) armRetry(roomId);
}
function cancelRetries() {
@@ -119,7 +119,7 @@ export function createThreadProjection<M extends ThreadMessage>(io: {
// dropped, and the reader is asked to read again: the refresh in between skipped it.
try {
const store = await read(item);
- retryAt.delete(cacheKey); settledRetry(roomId, cacheKey);
+ settledRetry(roomId, cacheKey);
if (store) { if (generation(roomId) === started) store(); wrote = true; }
} catch { if (generation(roomId) === started) scheduleRetry(roomId, cacheKey); else wrote = true; }
finally { inFlight.delete(cacheKey); }
@@ -205,6 +205,9 @@ export function createThreadProjection<M extends ThreadMessage>(io: {
invalidate(roomId: string, messageId: string) {
roomGenerations.set(roomId, (roomGenerations.get(roomId) ?? 0) + 1);
sources.delete(key(roomId, messageId));
+ // Failed preview pages have no cached replies to identify their root. Invalidate
+ // their backoff with this room generation too; other rooms keep their deadlines.
+ for (const cacheKey of failures.keys()) if (cacheKey.startsWith(`${roomId}:`)) settledRetry(roomId, cacheKey);
for (const [cacheKey, entry] of previews)
if (cacheKey.startsWith(`${roomId}:`) && entry.replies.some(reply => reply.id === messageId)) previews.delete(cacheKey);
},
diff --git a/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx b/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
index 7e9411e94d..c06e5dbab5 100644
--- a/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
+++ b/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
@@ -226,7 +226,10 @@ export function ChatThreadPanel({ group, rootId, quoteSources = group.messages,
const owner = getDataOwnerGeneration();
const failed = (code?: string) => {
if (!isDataOwnerGenerationCurrent(owner)) return;
- if (fromComposer) setUnposted(previous => new Set(previous).add(messageId));
+ if (code === 'SHARE_UNAVAILABLE') {
+ setUnposted(previous => { const next = new Set(previous); next.delete(messageId); return next; });
+ toast.error(t(chatErrorKey(code)));
+ } else if (fromComposer) setUnposted(previous => new Set(previous).add(messageId));
else toast.error(t(code ? chatErrorKey(code) : key('requestFailed')));
};
try {
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
index c919f14261..849304d177 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
@@ -2376,6 +2376,23 @@ describe('thread previews and posting to the main chat', () => {
expect(mocks.reply).toHaveBeenCalledTimes(1);
});
+ it.each([false, true])('drops an unavailable main-chat copy and reports why (after transient failure: %s)', async retrying => {
+ mocks.getBotGroup.mockResolvedValue({ ok: true, group: group([msg({ id: 'u1', content: '去哪玩' })]) });
+ if (retrying) mocks.shareToMain.mockResolvedValueOnce({ ok: false, errorCode: 'SERVICE_ERROR' });
+ mocks.shareToMain.mockResolvedValueOnce({ ok: false, errorCode: 'SHARE_UNAVAILABLE' });
+ const user = userEvent.setup();
+ renderView();
+ await user.click(await screen.findByRole('button', { name: 'bots.groupChat.server.startTopic' }));
+ const panel = await screen.findByRole('complementary');
+ await user.click(within(panel).getByRole('checkbox', { name: t('alsoSend') }));
+ await user.type(within(panel).getByRole('textbox'), 'both{Enter}');
+ if (retrying) await user.click(await within(panel).findByRole('button', { name: t('retryPost') }));
+ await waitFor(() => expect(mocks.toastError).toHaveBeenCalledWith(t('shareUnavailable')));
+ expect(within(panel).queryByRole('button', { name: t('retryPost') })).toBeNull();
+ expect(mocks.reply).toHaveBeenCalledTimes(1);
+ expect(mocks.shareToMain).toHaveBeenCalledTimes(retrying ? 2 : 1);
+ });
+
it('keeps one send identity across a failed reply and never posts to the main chat before it lands', async () => {
mocks.getBotGroup.mockResolvedValue({ ok: true, group: group([msg({ id: 'u1', content: '去哪玩' })]) });
mocks.reply.mockResolvedValueOnce({ ok: false, errorCode: 'SERVICE_ERROR' });
diff --git a/docs/product-rules/bot-group-chat.md b/docs/product-rules/bot-group-chat.md
index 088c95bff9..75f6b91a3f 100644
--- a/docs/product-rules/bot-group-chat.md
+++ b/docs/product-rules/bot-group-chat.md
@@ -560,9 +560,9 @@ Desktop 主聊天直接展示话题里的最新回复,并能把话题回复贴
遇到已删除回复占位时向前再读,最多 4 页。这些读取在后台进行,不拖住群页面:先显示数量,读完后刷新一次。
结果缓存到回复数变化,或其中某条被编辑、删除为止。读取失败时先只显示数量,然后按 30 秒、1、2、4、8 分钟自动重试,
最多 5 次,之后等下一条推送或重新打开群聊;同一群里多条读取失败各自按自己的到期时间重试,共用一个定时器、到点后接着排下一个;
- 切换账号或退出时取消。
+ 编辑或删除推送会清除该群旧的失败退避并立即重读,其他群的重试期限不变;切换账号或退出时取消。
- **同时发到主聊天**:话题输入框有「同时发到群聊」,默认不选、每次发送后恢复默认、切换话题不继承。先发话题回复,成功后再贴回主聊天;
- 贴回失败时回复保留在话题里,显示「重新发到群聊」,重试只重做贴回这一步,不重发回复。多条失败一起列出,
+ 贴回临时失败时回复保留在话题里,显示「重新发到群聊」,重试只重做贴回这一步,不重发回复。服务端确认来源不可贴回(`SHARE_UNAVAILABLE`)时移除该重试项并显示原因。多条临时失败一起列出,
按群和话题保存在本次运行中,切换话题再回来仍可重试;切换账号时清空,不把一个账号的待重试项交给另一个账号。回复失败时重试沿用原 `clientId`,回复送达前不会贴回。
- **贴回已有回复**:话题里每条回复(包括别人和伙伴的)在「更多」菜单里有「贴回群聊」。已贴过的回复显示「已贴到群聊」,点了回到主聊天里那条;
自己贴过的,菜单变为「在群聊中查看」。贴回接口返回的消息 ID 会保留下来;那条贴回在主群更早的记录里、还没加载时,会提示往上加载。