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(desktop): keep teammate task decisions actionable (#5004)

Chris Zhanga5b32cdd2a48
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
index 1479fa2402..ea33a4e059 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
@@ -3471,6 +3471,7 @@ describe('Bot Session task end-to-end runtime', () => {
>;
onResultReceiptPersisted?: () => Promise<void>;
onCompletionDispatched?: () => Promise<void>;
+ onInteractionDispatched?: () => Promise<void>;
} = {}) {
const accountReady = options.accountReady ?? (() => true);
const started: StartedTurn[] = [];
@@ -3666,6 +3667,9 @@ describe('Bot Session task end-to-end runtime', () => {
if (result.ok && params.clientId?.startsWith('bot-delegation-completion:')) {
await options.onCompletionDispatched?.();
}
+ if (result.ok && params.clientId?.startsWith('bot-delegation-interaction:')) {
+ await options.onInteractionDispatched?.();
+ }
return result;
});
@@ -6353,10 +6357,22 @@ describe('Bot Session task end-to-end runtime', () => {
kind: 'permission' as const,
requestId: 'permission-1',
toolName: 'write_file',
- input: { path: '/tmp/report.md' },
+ input: {
+ path: '/tmp/report.md',
+ headers: { 'X-Auth': 'opaque-private-credential' },
+ token: 'private-credential-value',
+ },
title: '写入报告',
};
await runtime.delegation.handleInteractionStart(started.childSessionId, request);
+ const wake = runtime.dispatch.mock.calls.find(([params]) =>
+ params.clientId === `bot-delegation-interaction:${started.delegationId}:${request.requestId}`,
+ )?.[0].message;
+ expect(wake).toContain('请求工具: write_file');
+ expect(wake).not.toContain('/tmp/report.md');
+ expect(wake).not.toContain('opaque-private-credential');
+ expect(wake).not.toContain('private-credential-value');
+ expect(wake).not.toContain('请求参数');
await expect(
runtime.delegation.getSessionTask('session-1', started.delegationId),
).resolves.toMatchObject({
@@ -6367,6 +6383,7 @@ describe('Bot Session task end-to-end runtime', () => {
pendingInteraction: {
requestId: 'permission-1',
kind: 'permission',
+ summary: '写入报告',
},
},
});
@@ -6393,6 +6410,44 @@ describe('Bot Session task end-to-end runtime', () => {
}
});
+ it('retries a pending approval wake-up in the replacement canonical task', async () => {
+ await seedPair();
+ vi.useFakeTimers();
+ let replacementSessionId: string | undefined;
+ const runtime = createDelegationRuntime({
+ onInteractionDispatched: async () => {
+ if (replacementSessionId) return;
+ h.sqlite!.prepare("DELETE FROM sessions WHERE id = 'session-1'").run();
+ const recovered = await invoke('local-db:bots:create-canonical-session', {
+ botId: 'bot-a', expectedCanonicalSessionId: null, expectedProfileVersion: 1,
+ });
+ replacementSessionId = recovered.canonicalSessionId as string;
+ },
+ });
+ try {
+ const task = await runtime.delegation.startSessionTask({
+ callerSessionId: 'session-1', objective: 'Wait for approval before writing.',
+ });
+ if (!task.ok) throw new Error('Task did not start');
+ const request = { kind: 'permission' as const, requestId: 'replacement-approval', toolName: 'write_file', input: {} };
+ await runtime.delegation.handleInteractionStart(task.childSessionId, request);
+ expect(replacementSessionId).toBeTruthy();
+ const wakeId = `bot-delegation-interaction:${task.delegationId}:${request.requestId}`;
+ expect(h.sqlite!.prepare('SELECT 1 FROM messages WHERE session_id = ? AND client_id = ?')
+ .get(replacementSessionId, wakeId)).toBeUndefined();
+
+ await vi.advanceTimersByTimeAsync(1_000);
+ expect(h.sqlite!.prepare('SELECT 1 FROM messages WHERE session_id = ? AND client_id = ?')
+ .get(replacementSessionId, wakeId)).toBeTruthy();
+ expect(runtime.started.filter(turn => turn.sessionId === replacementSessionId)).toHaveLength(1);
+ expect(await runtime.delegation.getSessionTask(replacementSessionId!, task.delegationId))
+ .toMatchObject({ task: { status: 'waiting', pendingInteraction: { requestId: request.requestId } } });
+ } finally {
+ runtime.dispose();
+ vi.useRealTimers();
+ }
+ });
+
it('does not charge user-decision time against the Session task deadline', async () => {
await seedPair();
vi.useFakeTimers();
diff --git a/apps/desktop/src/main/maker-ipc/botDelegationService.ts b/apps/desktop/src/main/maker-ipc/botDelegationService.ts
index 7bafd72a2f..f79899188a 100644
--- a/apps/desktop/src/main/maker-ipc/botDelegationService.ts
+++ b/apps/desktop/src/main/maker-ipc/botDelegationService.ts
@@ -1287,29 +1287,42 @@ export function createBotDelegationService(deps: BotDelegationServiceDeps) {
&& !pendingInteractions.get(row.id)?.decisionApplied;
if (!stillPending()
|| (row.childSessionId && heldSessionIds.has(row.childSessionId))) return;
- const requesterSessionId = await requesterLiveSessionId(
- row.requestingBotId,
- row.parentSessionId,
- );
- if (!stillPending() || (row.childSessionId && heldSessionIds.has(row.childSessionId))) return;
const message = [
`${UI_ACTION_TRIGGER_PREFIX}[任务需要你处理] task_id: ${row.id}`,
`类型: ${pending.request.kind}`,
pending.summary,
+ pending.request.kind === 'permission' ? `请求工具: ${pending.request.toolName}` : '',
'你是用户的代理。能按用户已表达的意图安全决定,就用 `message_session_task` 直接回答;拿不准才用一句人话问用户。不要让用户去子任务窗口处理,也不要复述内部编号。',
- ].join('\n\n');
- const dispatched = requesterSessionId
- ? await deps.dispatch({
- targetSessionId: requesterSessionId,
- message,
- persistedContent: message,
- clientId: `bot-delegation-interaction:${row.id}:${pending.requestId}`,
- }).catch(() => null)
- : null;
- if (dispatched?.ok) {
- clearInteractionRetryTimer(row.id);
- return;
+ ].filter(Boolean).join('\n\n');
+ try {
+ const requesterSessionId = await requesterLiveSessionId(row.requestingBotId, row.parentSessionId);
+ if (!stillPending() || (row.childSessionId && heldSessionIds.has(row.childSessionId))) return;
+ const dispatched = requesterSessionId
+ ? await deps.dispatch({
+ targetSessionId: requesterSessionId,
+ message,
+ persistedContent: message,
+ clientId: `bot-delegation-interaction:${row.id}:${pending.requestId}`,
+ })
+ : null;
+ if (dispatched?.ok) {
+ // Acceptance in a parent that was deleted or archived during dispatch
+ // does not wake its replacement. The client ID is stable per target,
+ // so retrying is safe even if the first send was merely queued.
+ const currentTarget = await requesterLiveSessionId(row.requestingBotId, row.parentSessionId);
+ if (currentTarget === requesterSessionId || !stillPending()) {
+ clearInteractionRetryTimer(row.id);
+ return;
+ }
+ }
+ } catch (error) {
+ log.warn('Bot task interaction wake-up deferred', {
+ delegationId: row.id,
+ requestId: pending.requestId,
+ error: error instanceof Error ? error.message : String(error),
+ });
}
+ if (!stillPending() || (row.childSessionId && heldSessionIds.has(row.childSessionId))) return;
clearInteractionRetryTimer(row.id);
const delay = Math.min(MAX_RETRY_DELAY_MS, 1_000 * 2 ** Math.min(attempt, 6));
const timer = setTimeout(() => {