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): preserve group replies across persistence and approval waits

Chris4ab149df482b
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 dac253ced6..20f9fc10a2 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
@@ -276,6 +276,32 @@ describe('Chat Server result delivery and refresh', () => {
expect(JSON.stringify(failures())).not.toContain('private path and token');
});
+ it('keeps an ordinary group turn alive while waiting for user approval', async () => {
+ let waiting = true;
+ deps.hasPendingInteraction = vi.fn(() => waiting);
+ await start();
+ await vi.advanceTimersByTimeAsync(6 * 60_000);
+ expect(deps.abortLane).not.toHaveBeenCalled();
+ expect(fixture.handle.mock.calls.filter(([, , body]) => body?.action === 'fail')).toEqual([]);
+ waiting = false;
+ await vi.advanceTimersByTimeAsync(15_000);
+ await service.settleLaneTurn(terminal);
+ expect(deliveries()).toHaveLength(1);
+ expect(deps.dispatch).toHaveBeenCalledOnce();
+ expect(deps.abortLane).not.toHaveBeenCalled();
+ });
+
+ it('still times out an ordinary group turn after its active runtime budget is exhausted', async () => {
+ deps.hasPendingInteraction = vi.fn(() => false);
+ await start();
+ await vi.advanceTimersByTimeAsync(6 * 60_000);
+ expect(deps.abortLane).toHaveBeenCalledExactlyOnceWith('lane');
+ const failures = fixture.handle.mock.calls.filter(([, , body]) => body?.action === 'fail');
+ expect(failures).toHaveLength(1);
+ expect(failures[0][2]).toMatchObject({ detail: 'cindy-runtime-error:RUNTIME_TIMEOUT' });
+ expect(deliveries()).toEqual([]);
+ });
+
it('rejects an old undispatched input instead of failing its replacement', async () => {
await start();
expect(await settleUndispatchedBotGroupTurn(service, 'lane', 'old-input', 'failed', 'MODEL_NOT_FOUND')).toBe(false);
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/sessionEventPipeline.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/sessionEventPipeline.test.ts
index 05cc3878d4..72438105d1 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/sessionEventPipeline.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/sessionEventPipeline.test.ts
@@ -1817,6 +1817,43 @@ describe('Bot adapters in the shared event pipeline', () => {
await h.dispose();
});
+ it('waits for the streamed group reply to persist and keeps the finished input identity', async () => {
+ const h = harness();
+ let finishWrite!: () => void;
+ const writing = new Promise<void>(resolve => { finishWrite = resolve; });
+ effects.fn('drainPersistQueue').mockReturnValue(writing);
+ effects.fn('consumeLastAssistantPersistId').mockReturnValue('group-reply');
+ const settleLaneTurn = vi.fn(async () => true);
+ (h.deps as unknown as { botGroupChatServiceHolder: unknown }).botGroupChatServiceHolder = { settleLaneTurn };
+ h.deps.agentInputCoordinatorHolder.getActiveInputClientId.mockReturnValue('bot-group:g1:turn:bot-a');
+ h.emit(event('done', { result: '' }));
+ h.deps.agentInputCoordinatorHolder.getActiveInputClientId.mockReturnValue('bot-group:g1:next:bot-a');
+ await microtasks();
+ expect(settleLaneTurn).not.toHaveBeenCalled();
+ finishWrite();
+ await microtasks();
+ expect(settleLaneTurn).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({
+ sessionId: 'task', activeInputClientId: 'bot-group:g1:turn:bot-a', outcome: 'done',
+ resultText: '', resultMessageClientId: 'group-reply',
+ }));
+ await h.dispose();
+ });
+
+ it.each(['reply', 'failure'])('does not delay a group %s that needs no persisted-text fallback', async kind => {
+ const h = harness();
+ let finishWrite!: () => void;
+ effects.fn('drainPersistQueue').mockReturnValue(new Promise<void>(resolve => { finishWrite = resolve; }));
+ effects.fn('consumeLastAssistantPersistId').mockReturnValue('group-reply');
+ const settleLaneTurn = vi.fn(async () => true);
+ (h.deps as unknown as { botGroupChatServiceHolder: unknown }).botGroupChatServiceHolder = { settleLaneTurn };
+ h.emit(kind === 'reply' ? event('done', { result: 'Ready' }) : event('error', { isTerminal: true, errorStatus: 401 }));
+ await microtasks();
+ expect(settleLaneTurn).toHaveBeenCalledOnce();
+ finishWrite();
+ await microtasks();
+ await h.dispose();
+ });
+
it.each([
[{ message: 'Authorization: [REDACTED]', errorStatus: 429, usageLimit: true }, 'QUOTA_EXCEEDED'],
[{ errorStatus: 401 }, 'AUTH_REQUIRED'],
diff --git a/apps/desktop/src/main/maker-ipc/chatServer.ts b/apps/desktop/src/main/maker-ipc/chatServer.ts
index a2a743d8a1..cc94edc279 100644
--- a/apps/desktop/src/main/maker-ipc/chatServer.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServer.ts
@@ -964,7 +964,8 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
if (checking.has(run) || running.get(run.execution.bot_id) !== run) return;
checking.add(run);
try {
- const paused = !!run.plan && !!deps.hasPendingInteraction?.(run.sessionId);
+ // Ordinary group replies can wait for approval too, just like plan steps.
+ const paused = !!deps.hasPendingInteraction?.(run.sessionId);
if (paused) run.pauseStarted ??= Date.now();
else if (run.pauseStarted !== undefined) { run.started += Date.now() - run.pauseStarted; run.pauseStarted = undefined; }
const timedOut = !paused && Date.now() - run.started > (run.plan ? 2 * 60 * 60 * 1000 : 300000);
diff --git a/apps/desktop/src/main/maker-ipc/sessionEventTerminal.ts b/apps/desktop/src/main/maker-ipc/sessionEventTerminal.ts
index c635047a27..1394048005 100644
--- a/apps/desktop/src/main/maker-ipc/sessionEventTerminal.ts
+++ b/apps/desktop/src/main/maker-ipc/sessionEventTerminal.ts
@@ -656,21 +656,29 @@ export function finishSessionTerminalEvent(
})();
if (deps.botGroupChatServiceHolder) {
const groupDoneData = event.data as { result?: unknown } | null;
- void deps.botGroupChatServiceHolder
- .settleLaneTurn({
+ const groupService = deps.botGroupChatServiceHolder;
+ const groupTerminal = {
+ sessionId: session.id,
+ activeInputClientId: groupLaneInputClientId,
+ outcome: isTerminalTurnErrorEvent(event) ? 'error' as const : 'done' as const,
+ resultText: typeof groupDoneData?.result === 'string' ? groupDoneData.result : '',
+ resultMessageClientId: turnAssistantPersistId,
+ ...(isTerminalTurnErrorEvent(event) ? { failureCode: botGroupRuntimeFailureCode(event.data) } : {}),
+ };
+ void (async () => {
+ // The fallback reader in both local and server groups needs the final
+ // streamed block on disk. Flush only queued that write; reading now can
+ // turn a real reply into NO_REPLY. Keep the captured input across the wait.
+ if (groupTerminal.outcome === 'done' && !groupTerminal.resultText.trim() && groupTerminal.resultMessageClientId) {
+ await drainPersistQueue();
+ }
+ await groupService.settleLaneTurn(groupTerminal);
+ })().catch((error) => {
+ deps.log.warn('Bot group lane terminal settlement failed', {
sessionId: session.id,
- activeInputClientId: groupLaneInputClientId,
- outcome: isTerminalTurnErrorEvent(event) ? 'error' : 'done',
- resultText: typeof groupDoneData?.result === 'string' ? groupDoneData.result : '',
- resultMessageClientId: turnAssistantPersistId,
- ...(isTerminalTurnErrorEvent(event) ? { failureCode: botGroupRuntimeFailureCode(event.data) } : {}),
- })
- .catch((error) => {
- deps.log.warn('Bot group lane terminal settlement failed', {
- sessionId: session.id,
- error: error instanceof Error ? error.message : String(error),
- });
+ error: error instanceof Error ? error.message : String(error),
});
+ });
}
if (providerGroupHeldWorkerTerminal) {
// 已与 error 行一起暂存,由那次登记结算。