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): 被顶替发送的安排取消连续失败后按退避持续重试

Chris65673221296f
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 92dcf958c6..6a9bd253f4 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
@@ -1739,6 +1739,43 @@ describe('Chat Server result delivery and refresh', () => {
expect(deps.decidePlan).toHaveBeenCalledOnce();
});
+ it('keeps retrying an overtaken send\'s cancel with backoff until it lands', async () => {
+ const base = fixture.handle.getMockImplementation()!;
+ let finishFirst!: () => void;
+ const cancels: string[] = [];
+ let posts = 0;
+ fixture.handle.mockImplementation((route, method, data) => {
+ if (route === '/me') return { body: { actor: { id: selfId }, capabilities: { groupDiscussionParity: 1 } } };
+ if (route.endsWith('/messages') && method === 'POST') {
+ posts += 1;
+ const body = { id: `5000000${posts}-0000-4000-8000-000000000001` };
+ return posts === 1 ? new Promise(resolve => { finishFirst = () => resolve({ body }); }) : { body };
+ }
+ if (route.endsWith('/cancel-planning')) {
+ cancels.push(route);
+ // The cancel and its immediate retry both fail, and so does the first backoff retry.
+ return cancels.length <= 3 ? { status: 503, body: { error: { code: 'UNAVAILABLE' } } } : { body: {} };
+ }
+ return base(route, method, data);
+ });
+ deps.decidePlan = vi.fn(() => new Promise(() => {})) as typeof deps.decidePlan;
+ const first = send('latency-plan-f');
+ await vi.advanceTimersByTimeAsync(0);
+ expect(await send('latency-plan-g')).toMatchObject({ ok: true });
+ finishFirst();
+ expect(await first).toMatchObject({ ok: true });
+ await vi.advanceTimersByTimeAsync(0);
+ expect(cancels).toHaveLength(2);
+ // No newer message or control action follows; the deferred message is still released.
+ await vi.advanceTimersByTimeAsync(2_000);
+ expect(cancels).toHaveLength(3);
+ await vi.advanceTimersByTimeAsync(4_000);
+ expect(cancels).toHaveLength(4);
+ await vi.advanceTimersByTimeAsync(120_000);
+ const cancel = `/conversations/${roomId}/messages/50000001-0000-4000-8000-000000000001/cancel-planning`;
+ expect(cancels).toEqual([cancel, cancel, cancel, cancel]);
+ });
+
it('shows the organizer as a planning speaker while the decision runs and removes it after', async () => {
let decide!: (value: null) => void;
deps.decidePlan = vi.fn(() => new Promise(resolve => { decide = resolve; })) as typeof deps.decidePlan;
diff --git a/apps/desktop/src/main/maker-ipc/chatServer.ts b/apps/desktop/src/main/maker-ipc/chatServer.ts
index 21247e6beb..5e19d215d0 100644
--- a/apps/desktop/src/main/maker-ipc/chatServer.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServer.ts
@@ -298,14 +298,32 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
if (previousSourceId) await cancelPlanning(roomId, previousSourceId);
for (const [sourceId, failed] of pendingPlanCancels.get(roomId) ?? []) await cancelPlanning(roomId, sourceId, failed);
}
- /** A failed cancel stays pending and is retried once here (and by the next intent). */
+ /**
+ * A failed cancel stays pending and is retried once here, then with backoff until it lands
+ * (the next intent flushes it too): nothing else would release the deferred message.
+ */
async function cancelPlanningWithRetry(roomId: string, cancel: () => Promise<void>) {
try { await cancel(); }
catch (error) {
deps.log?.warn('Group arrangement cancel will retry', { groupId: roomId, ...chatErrorDiagnostic(error) });
- await flushPlanCancels(roomId).catch(retryError => deps.log?.warn('Group arrangement cancel failed', { groupId: roomId, ...chatErrorDiagnostic(retryError) }));
+ await flushPlanCancels(roomId).catch(retryError => {
+ deps.log?.warn('Group arrangement cancel failed', { groupId: roomId, ...chatErrorDiagnostic(retryError) });
+ schedulePlanCancelRetry(roomId);
+ });
}
}
+ const planCancelRetries = new Map<string, ReturnType<typeof setTimeout>>();
+ function schedulePlanCancelRetry(roomId: string, attempt = 1) {
+ if (planCancelRetries.has(roomId) || !current()) return;
+ planCancelRetries.set(roomId, setTimeout(() => {
+ planCancelRetries.delete(roomId);
+ if (!current() || !pendingPlanCancels.get(roomId)?.size) return;
+ void flushPlanCancels(roomId).catch(error => {
+ deps.log?.warn('Group arrangement cancel failed', { groupId: roomId, ...chatErrorDiagnostic(error) });
+ schedulePlanCancelRetry(roomId, attempt + 1);
+ });
+ }, Math.min(60_000, 1_000 * 2 ** Math.min(attempt, 6))));
+ }
async function replacePlanIntent(roomId: string, version = ++nextIntentVersion) {
const { intent, previousSourceId } = installPlanIntent(roomId, version);
await flushPlanCancels(roomId, previousSourceId);
@@ -1980,6 +1998,8 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
attachmentRefreshes.clear();
for (const pending of pendingChanges.values()) clearTimeout(pending);
pendingChanges.clear();
+ for (const retry of planCancelRetries.values()) clearTimeout(retry);
+ planCancelRetries.clear();
for (const run of running.values()) {
run.releaseToolAuthority?.();
if (run.sessionId) void deps.abortLane(run.sessionId).catch(() => undefined);