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): preserve planning when a later send fails

Chrisd3f62e4f762d
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServerParity.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServerParity.test.ts
index 47048c85d5..68a92c14fe 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServerParity.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServerParity.test.ts
@@ -105,7 +105,6 @@ describe('server group feature parity', () => {
it.each([
['ordinary request', {}, undefined],
['@all', { mentions: { all: true, botIds: [] } }, undefined],
- ['one available member', {}, members.slice(0, 1)],
])('asks the organizer for %s without changing server mentions', async (_name, input, roster) => {
arrange(undefined, roster);
expect((await send(input)).ok).toBe(true); await flush();
@@ -115,6 +114,36 @@ describe('server group feature parity', () => {
expect(posted.deferExecution).toBe(true);
expect(posted.mentions).toEqual(_name === '@all' ? [botId, secondBotId] : []);
});
+ it('returns one available member to ordinary discussion using the real decision parser', async () => {
+ arrange(undefined, members.slice(0, 1));
+ const { parsePlanDecision } = await import('../botGroupDivision.js');
+ deps.decidePlan = vi.fn(async input => parsePlanDecision(JSON.stringify({ needsPlan: true, steps: [{ botId, task: 'Make a draft' }] }), input.mode, new Set(input.members.map(m => m.botId))));
+ expect((await send()).ok).toBe(true); await flush();
+ expect(deps.decidePlan).toHaveBeenCalledWith(expect.objectContaining({ mode: 'auto' }), expect.any(AbortSignal));
+ expect(planPosts()).toHaveLength(0);
+ expect(fixture.handle.mock.calls.some(([route]) => route.endsWith('/continue'))).toBe(true);
+ });
+ it.each(['prepare', 'upload', 'submit'])('preserves the pending decision when a later send fails at %s', async stage => {
+ arrange();
+ let finish!: (value: { needsPlan: true; steps: typeof steps }) => void;
+ const decide = vi.fn<NonNullable<BotGroupChatServiceDeps['decidePlan']>>(() => new Promise(resolve => { finish = resolve; }));
+ deps.decidePlan = decide;
+ await send(); await flush();
+ const signal = decide.mock.calls[0]![1];
+ deps.prepareAttachments = vi.fn(async () => stage === 'prepare'
+ ? { ok: false as const, errorCode: 'INVALID_PARAMS' as const, message: 'invalid' }
+ : { ok: true as const, attachments: [], commit: vi.fn(), discard: vi.fn(async () => {}) });
+ if (stage === 'upload') fixture.upload.mockRejectedValueOnce(new Error('MEDIA_UPLOAD_FAILED'));
+ if (stage === 'submit') {
+ const base = fixture.handle.getMockImplementation()!;
+ fixture.handle.mockImplementation((route, method, data) => route.endsWith('/messages') && method === 'POST'
+ ? { status: 500, body: { error: { code: 'INTERNAL' } } } : base(route, method, data));
+ }
+ expect((await send({ clientId: `failed-${stage}`, attachments: [{}] })).ok).toBe(false);
+ expect(signal.aborted).toBe(false);
+ finish({ needsPlan: true, steps }); await flush();
+ expect(planPosts()).toHaveLength(1);
+ });
it.each([false, true])('revises a proposed plan, including forced=%s, without losing its steps', async division => {
arrange(proposed);
expect((await send({ division })).ok).toBe(true); await flush();
diff --git a/apps/desktop/src/main/maker-ipc/chatServer.ts b/apps/desktop/src/main/maker-ipc/chatServer.ts
index 1e5e430cfe..77f63cc373 100644
--- a/apps/desktop/src/main/maker-ipc/chatServer.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServer.ts
@@ -129,6 +129,8 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
const planning = new Map<string, { botId: string; controller: AbortController }>();
// Invalidate even a send still preparing/uploading its attachments. Its delayed
// receipt must not start an arrangement after a newer message or control action.
+ let nextIntentVersion = 0;
+ const acceptedIntentVersions = new Map<string, number>();
type PlanIntent = { sourceId?: string };
const planIntents = new Map<string, PlanIntent>();
let groupDiscussionParity = false;
@@ -142,7 +144,8 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
pending.delete(sourceId);
if (!pending.size) pendingPlanCancels.delete(roomId);
}
- async function replacePlanIntent(roomId: string) {
+ async function replacePlanIntent(roomId: string, version = ++nextIntentVersion) {
+ acceptedIntentVersions.set(roomId, version);
const pending = planning.get(roomId);
pending?.controller.abort();
planning.delete(roomId);
@@ -870,7 +873,8 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
mentions: z.object({ all: z.boolean(), botIds: z.array(z.string()) }), division: z.boolean().optional(), attachments: z.array(z.unknown()).optional() }).parse(input);
i.groupId = await resolveGroup(i.groupId);
if (!i.text.trim() && !i.attachments?.length) throw new Error('INVALID_INPUT');
- const intent = await replacePlanIntent(i.groupId);
+ // An attempted send must not cancel a valid decision until its message exists.
+ const intentVersion = ++nextIntentVersion;
const s = await snapshot(i.groupId);
const plans = await api<ServerPlan[]>(`/conversations/${i.groupId}/plans`);
const openPlan = plans.find(p => ['proposed','running','waiting'].includes(p.status));
@@ -897,8 +901,10 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
throw error;
}
if (prepared?.ok) prepared.commit();
+ const intent = intentVersion > (acceptedIntentVersions.get(i.groupId) ?? 0)
+ ? await replacePlanIntent(i.groupId, intentVersion) : null;
if (shouldPlan) {
- if (planIntents.get(i.groupId) !== intent) await cancelPlanning(i.groupId, result.id);
+ if (!intent || planIntents.get(i.groupId) !== intent) await cancelPlanning(i.groupId, result.id);
else {
intent.sourceId = result.id;
void decideArrangement(s, result.id, i.text, i.division === true, prepared?.ok ? prepared.attachments : [], intent).catch(() => undefined);