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): settle a failed lane setup without waiting for the turn's downloads

Chrisc7c8e9919198
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 a04c1dec16..a50422ba87 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
@@ -489,6 +489,22 @@ describe('Chat Server result delivery and refresh', () => {
expect(JSON.stringify(failures)).not.toContain('private preparation details');
});
+ it('settles a failed lane preparation at once instead of waiting for the turn\'s attachment downloads', async () => {
+ fixture.profiles = [{ id: 'local-bot', displayName: 'Bot', avatar: null, status: 'active' }];
+ const files = Array.from({ length: 12 }, (_, n) => ({ type: 'media', mediaId: `60000000-0000-4000-8000-${String(n + 1).padStart(12, '0')}`, caption: `f${n}.pdf` }));
+ const source = { id: execution.source_message_id, seq: '1', authorId: selfId, author: { kind: 'human', name: 'Me' },
+ content: [{ type: 'text', text: 'Read these' }, ...files], createdAt: new Date().toISOString(), deleted: false, threadRootId: null };
+ fixture.handle.mockImplementation(route => route.includes('/messages?') ? { body: [source] } : response(route));
+ // Slow media never finishes within this test.
+ fixture.download.mockImplementation(() => new Promise(() => {}));
+ deps.ensureLane = vi.fn(async () => ({ ok: false as const, errorCode: 'NO_MODEL', message: 'private preparation details' }));
+ await vi.advanceTimersByTimeAsync(2000);
+ const failures = fixture.handle.mock.calls.filter(([, , body]) => body?.action === 'fail');
+ expect(failures).toHaveLength(1);
+ expect(failures[0][2]).toMatchObject({ detail: 'cindy-runtime-error:MODEL_UNAVAILABLE' });
+ expect(fixture.download.mock.calls.length).toBeLessThan(files.length);
+ });
+
it.each([
['authentication_failed private credential', 'AUTH_REQUIRED'],
['ECONNRESET private endpoint', 'NETWORK_ERROR'],
diff --git a/apps/desktop/src/main/maker-ipc/chatServer.ts b/apps/desktop/src/main/maker-ipc/chatServer.ts
index e03fe323e1..1e6a9eb415 100644
--- a/apps/desktop/src/main/maker-ipc/chatServer.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServer.ts
@@ -1248,6 +1248,8 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
if (s.room.kind === 'direct') throw new Error('INVALID_EXECUTION_TRIGGER');
// Lane setup (local) and the turn's context (server reads, attachment downloads) are
// independent; both finish before the single lease check that precedes the dispatch.
+ // A failed setup settles at once: no further downloads start and none are waited for.
+ let laneFailed = false;
const laneSetup = (async () => {
// Metadata only: the server remains the sole message store and scheduler.
if (!metadata.has(s.room.id)) metadata.set(s.room.id, (async () => {
@@ -1317,6 +1319,7 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
await deps.syncLanePermission?.(lane.sessionId, bot.id);
return lane;
})();
+ laneSetup.catch(() => { laneFailed = true; });
const contextRead = (async () => {
const history = await historyRead;
let page = [...history];
@@ -1380,10 +1383,13 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
// The first 40 files in that order that download are attached, as when they were read
// one by one; a few download at once. A failed file frees its place for the next one.
let next = 0;
- while (next < candidates.length && attachments.length < 40) {
+ while (!laneFailed && next < candidates.length && attachments.length < 40) {
const batch = candidates.slice(next, next + 40 - attachments.length);
next += batch.length;
- const results = await settleBounded(batch, TURN_DOWNLOAD_CONCURRENCY, candidate => media.download(s.room.id, id.parse(candidate.mediaId)));
+ const results = await settleBounded(batch, TURN_DOWNLOAD_CONCURRENCY, async candidate => {
+ if (laneFailed) throw new Error('LANE_FAILED');
+ return media.download(s.room.id, id.parse(candidate.mediaId));
+ });
results.forEach((result, index) => {
if (result.status === 'fulfilled') { attachments.push(result.value); mediaDelivery.set(batch[index]!.mediaId, 'included'); }
else missingAttachments.push(batch[index]!.name);
@@ -1392,11 +1398,10 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
for (const candidate of candidates.slice(next)) missingAttachments.push(candidate.name);
return { history, source, threadRootId, thread, focusMessages, attachments, missingAttachments, mediaDelivery };
})();
- const [laneResult, contextResult] = await Promise.allSettled([laneSetup, contextRead]);
- if (laneResult.status === 'rejected') throw laneResult.reason;
- if (contextResult.status === 'rejected') throw contextResult.reason;
- const lane = laneResult.value;
- const { history, source, threadRootId, thread, focusMessages, attachments, missingAttachments, mediaDelivery } = contextResult.value;
+ // Observed here so a read that fails after a failed setup is not an unhandled rejection.
+ contextRead.catch(() => undefined);
+ const lane = await laneSetup;
+ const { history, source, threadRootId, thread, focusMessages, attachments, missingAttachments, mediaDelivery } = await contextRead;
if (!current()) throw new Error('OWNER_CHANGED');
// A quote resolves against every message this run read; otherwise its card snapshot stands.
const readMessages = [...history, ...focusMessages, ...(thread?.background ?? [])];