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(provider-group): 等待暂存群回复落库后再结算

guyong66637b703335
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botGroupChatService.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botGroupChatService.test.ts
index a58c070b9b..1fcad2604e 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botGroupChatService.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botGroupChatService.test.ts
@@ -24,8 +24,12 @@ import {
createBotGroupChatService,
resolveGroupMentions,
rotateResponders,
+ settleBotGroupLaneTerminal,
type BotGroupChatServiceDeps,
+ type BotGroupLaneTerminal,
} from '../botGroupChatService.js';
+import { createProviderGroupHeldWorkerTerminals } from '../../provider-group/heldWorkerTerminals.js';
+import { AutoResumeBookkeeping } from '../autoResumeBookkeeping.js';
import {
buildPlanDecisionPrompt,
buildPlanStepBrief,
@@ -233,6 +237,64 @@ describe('botGroupChatService', () => {
]);
});
+ // #6671 / bot-group-chat §4.3–4.4 (unit): a held done-only turn can have
+ // streamed text still queued for persistence. Existing tests covered the
+ // ordinary event path, not release/handback through the real recovery stores.
+ // Read the actual group result so an early empty DB read cannot silently eat it.
+ it.each(['group', 'generic'] as const)('keeps a streamed reply pending until persistence when %s recovery releases it', async recovery => {
+ let written = false;
+ let finishWrite!: () => void;
+ const writing = new Promise<void>(resolve => { finishWrite = () => { written = true; resolve(); }; });
+ const drain = vi.fn(() => writing);
+ const readReplyText = vi.fn(async () => written ? '落库后的实际回复' : null);
+ const harness = createHarness((_bot, _prompt, index) => index === 0 ? null : 'NO_REPLY', { readReplyText });
+ const groupId = await createGroup(harness);
+ await harness.service.sendMessage({ groupId, text: '继续讨论', mentions: { all: true, botIds: [] }, clientId: 'source-input' });
+ await vi.waitFor(() => expect(harness.dispatches).toHaveLength(3));
+ const call = harness.dispatches[0]!;
+ let settlement: Promise<boolean> | undefined;
+ const deliver = (terminal: BotGroupLaneTerminal) => {
+ settlement = settleBotGroupLaneTerminal(harness.service, terminal, drain);
+ };
+ const held = createProviderGroupHeldWorkerTerminals<BotGroupLaneTerminal>({
+ ownerKey: () => 'account', deliver: (_sessionId, terminal) => deliver(terminal),
+ });
+ const book = new AutoResumeBookkeeping({
+ persistSuppressedError: vi.fn(), markOutcome: vi.fn(),
+ rollbackGuardPendingResume: vi.fn(), abandonTakeover: vi.fn(),
+ finalizeBotGroupSuppressedTerminal: deliver,
+ });
+ held.stash(call.sessionId, 3, {
+ sessionId: call.sessionId, activeInputClientId: call.clientId,
+ outcome: 'done', resultText: '', resultMessageClientId: 'queued-reply',
+ });
+ try {
+ if (recovery === 'generic') {
+ book.stashSuppressedError(call.sessionId, { reason: 'upstream-overload' });
+ book.stashBotGroupSuppressedTerminal(call.sessionId, held.takeForRecovery(call.sessionId, 3)!);
+ book.surfaceSuppressedError(call.sessionId);
+ book.surfaceSuppressedError(call.sessionId);
+ }
+ held.release(call.sessionId, 3);
+ held.release(call.sessionId, 3);
+ await Promise.resolve();
+ expect(readReplyText.mock.calls.length).toBe(0);
+ const waiting = await harness.service.getGroup(groupId);
+ expect(waiting.ok && waiting.group.speakingBotIds).toContain(call.botId);
+ finishWrite();
+ await settlement;
+ const group = await waitForIdle(harness, groupId);
+ expect(drain).toHaveBeenCalledOnce();
+ expect(readReplyText).toHaveBeenCalledExactlyOnceWith(call.sessionId, 'queued-reply');
+ expect(group.messages.filter(message => message.authorKind === 'bot').map(message => message.content))
+ .toEqual(['落库后的实际回复']);
+ } finally {
+ finishWrite();
+ await settlement;
+ await waitForIdle(harness, groupId);
+ }
+ });
+
it('in sequential mode lets every member answer in turn, rotates the next circle, and ends when a circle is silent', async () => {
const replies: Record<string, string[]> = {
mimi: ['先定个大框架', 'NO_REPLY'],
diff --git a/apps/desktop/src/main/maker-ipc/botGroupChatService.ts b/apps/desktop/src/main/maker-ipc/botGroupChatService.ts
index 20ff08bafc..7492cbbba0 100644
--- a/apps/desktop/src/main/maker-ipc/botGroupChatService.ts
+++ b/apps/desktop/src/main/maker-ipc/botGroupChatService.ts
@@ -110,6 +110,18 @@ export interface BotGroupLaneTerminal {
undispatched?: boolean;
}
+/** Ordinary and recovery-held replies must read their fallback text after queued writes. */
+export async function settleBotGroupLaneTerminal(
+ service: Pick<BotGroupChatService, 'settleLaneTurn'>,
+ terminal: BotGroupLaneTerminal,
+ drainPersistQueue: () => Promise<void>,
+): Promise<boolean> {
+ if (terminal.outcome === 'done' && !terminal.resultText.trim() && terminal.resultMessageClientId) {
+ await drainPersistQueue();
+ }
+ return service.settleLaneTurn(terminal);
+}
+
export interface BotGroupChatServiceDeps {
/**
* With `plan`, the Bot's 分工 Session for that plan (bot-group-chat.md §7.4); with
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 898d6bd535..97438af8d7 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -600,7 +600,7 @@ import {
createBotDirectMessageService,
type BotDirectMessageService,
} from './botDirectMessageService.js';
-import { createBotGroupChatService, type BotGroupChatService, type BotGroupChatServiceDeps, type BotGroupLaneTerminal } from './botGroupChatService.js';
+import { createBotGroupChatService, settleBotGroupLaneTerminal, type BotGroupChatService, type BotGroupChatServiceDeps, type BotGroupLaneTerminal } from './botGroupChatService.js';
import { settleUndispatchedBotGroupTurn } from './botGroupRuntimeFailure.js';
import { withChatServer } from './chatServer.js';
import { companionHomes, isCompanionShadowed } from '../companion-cloud/homes.js';
@@ -1951,7 +1951,8 @@ function settleDeferredProductTurnFailure(sessionId: string): void {
}
function settleDeferredBotGroupTerminal(terminal: BotGroupLaneTerminal): void {
- void botGroupChatServiceHolder?.settleLaneTurn(terminal).catch(() =>
+ if (!botGroupChatServiceHolder) return;
+ void settleBotGroupLaneTerminal(botGroupChatServiceHolder, terminal, drainPersistQueue).catch(() =>
log.warn('Bot group recovery failure settlement failed'));
}
diff --git a/apps/desktop/src/main/maker-ipc/sessionEventTerminal.ts b/apps/desktop/src/main/maker-ipc/sessionEventTerminal.ts
index bf925d83f5..827532f11e 100644
--- a/apps/desktop/src/main/maker-ipc/sessionEventTerminal.ts
+++ b/apps/desktop/src/main/maker-ipc/sessionEventTerminal.ts
@@ -2,7 +2,7 @@ import { reviewTeammateLearning } from './botLearningReview.js';
import { botLearningTracker } from './botLearningTracker.js';
import { learningTurnIdentity } from './botLearningFeedback.js';
import type { BotDelegationService } from './botDelegationService.js';
-import type { BotGroupChatService, BotGroupLaneTerminal } from './botGroupChatService.js';
+import { settleBotGroupLaneTerminal, type BotGroupChatService, type BotGroupLaneTerminal } from './botGroupChatService.js';
import type { AgentEvent, Session } from '@cindy/maker-core';
// sidebar-card-mode: turn-done 后刷新列表预览,并按需生成置顶卡片摘要
@@ -713,15 +713,7 @@ export function finishSessionTerminalEvent(
!(providerGroupDoneHoldId !== null && deps.stashProviderGroupHeldWorkerTerminal)
) {
const groupService = deps.botGroupChatServiceHolder;
- 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) => {
+ void settleBotGroupLaneTerminal(groupService, groupTerminal, drainPersistQueue).catch((error) => {
deps.log.warn('Bot group lane terminal settlement failed', {
sessionId: session.id,
error: error instanceof Error ? error.message : String(error),