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): 任务意外关闭后继续派发排队消息

Dashbd14716417c6
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts
index a62c465eb3..4b1c052272 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts
@@ -4677,6 +4677,66 @@ describe('AgentInputCoordinator send transaction', () => {
expect(capturedSignal?.aborted).toBe(true);
});
+ describe('unexpected close without a terminal event', () => {
+ // 远端 Agent 断连 / 进程退出时 Session 自行关闭,不发 done:排队消息只能靠
+ // close 收口时唤醒,否则会静默滞留到用户再次操作。
+ async function closeWithQueuedInput(
+ sid: string,
+ close: (h: ReturnType<typeof createHarness>) => void,
+ beforeClose?: (h: ReturnType<typeof createHarness>) => void,
+ ) {
+ const h = createHarness();
+ h.coordinator.enqueue(sid, makeItem('q-running', 'first'));
+ await flush();
+ h.coordinator.enqueue(sid, makeItem('q-waiting', 'second'));
+ await flush();
+ expect(h.sendToAgent).toHaveBeenCalledTimes(1);
+ expect(h.coordinator.getProjection(sid).pendingQueue.map((item) => item.clientId)).toEqual([
+ 'q-waiting',
+ ]);
+ beforeClose?.(h);
+ h.setRunning(false);
+ close(h);
+ await flush();
+ return h;
+ }
+
+ it('dispatches the queued head when the close asks to wake queued input', async () => {
+ const sid = 'unexpected-close-wakes-queue';
+ const h = await closeWithQueuedInput(sid, (harness) =>
+ harness.coordinator.onSessionClosed(sid, { wakeQueuedInput: true }),
+ );
+
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ expect(h.sendToAgent.mock.calls[1]?.[1]).toEqual({ type: 'user', content: 'second' });
+ expect(h.coordinator.getProjection(sid).pendingQueue).toEqual([]);
+ });
+
+ it('keeps the queue waiting on a plain close', async () => {
+ const sid = 'plain-close-keeps-queue';
+ const h = await closeWithQueuedInput(sid, (harness) => harness.coordinator.onSessionClosed(sid));
+
+ expect(h.sendToAgent).toHaveBeenCalledTimes(1);
+ expect(h.coordinator.getProjection(sid).pendingQueue.map((item) => item.clientId)).toEqual([
+ 'q-waiting',
+ ]);
+ });
+
+ it('does not dispatch a queue the user paused with Stop', async () => {
+ const sid = 'unexpected-close-respects-paused-queue';
+ const h = await closeWithQueuedInput(
+ sid,
+ (harness) => harness.coordinator.onSessionClosed(sid, { wakeQueuedInput: true }),
+ (harness) => harness.coordinator.stop(sid, { keepQueue: true, pauseQueue: true }),
+ );
+
+ expect(h.sendToAgent).toHaveBeenCalledTimes(1);
+ const projection = h.coordinator.getProjection(sid);
+ expect(projection.queuePaused).toBe(true);
+ expect(projection.pendingQueue.map((item) => item.clientId)).toEqual(['q-waiting']);
+ });
+ });
+
it('releases compact active turn when close races before dispatch outcome', async () => {
const h = createHarness();
const sid = 'compact-close-before-dispatch-outcome';
diff --git a/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts b/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
index ef58c8a226..1e74550fc1 100644
--- a/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
+++ b/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
@@ -4156,10 +4156,18 @@ export class AgentInputCoordinator {
* input signal(#1930),但**其余清理必须照常**(activeTurn / steer / queue
* 状态不能残留,否则 rebuild 失败或 close 后不 rebuild 时 coordinator
* 残留旧状态阻塞后续发送)。
+ * @param opts.wakeQueuedInput 为 true 时,收口后若仍有用户排队消息就重新 drain。
+ * 用于非请求关闭(进程退出 / 远端 Agent 断连):这类关闭不带 done 事件,
+ * turn-done 唤醒永远不会来,队首会静默滞留到用户再次操作。drain 走
+ * lazy create-session 重建运行时;暂停、recovery 等 gate 照常拦截。
*/
onSessionClosed(
sessionId: string,
- opts?: { preserveInputBoundary?: boolean; preserveAutoResumeIntent?: boolean },
+ opts?: {
+ preserveInputBoundary?: boolean;
+ preserveAutoResumeIntent?: boolean;
+ wakeQueuedInput?: boolean;
+ },
): void {
const state = this.getState(sessionId);
// Planned-upgrade suppression is per Session incarnation. Replacement
@@ -4224,6 +4232,8 @@ export class AgentInputCoordinator {
// Old provider busy is gone with this Session. The 10s SESSION_RUNNING
// retry was cleared above; wake the hidden CONTINUE onto the replacement.
this.scheduleDrain(sessionId, 'session-closed-preserved-auto-resume');
+ } else if (opts?.wakeQueuedInput && state.pendingQueue.length > 0) {
+ this.scheduleDrain(sessionId, 'session-closed-unexpected');
}
}
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 33f6588fc9..3d4b9b2b5a 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -5328,7 +5328,12 @@ const sessionBindings = createSessionBindingLifecycle<WiredSession, WiredSession
// preserves the signal currently driving that rebuild, not the old queue.
shouldPreserveInputBoundary: () => rehydrateCloseSuppression.isSuppressed(session.id),
closeInputCoordinator: (options) => {
- agentInputCoordinatorHolder?.onSessionClosed(session.id, options);
+ agentInputCoordinatorHolder?.onSessionClosed(session.id, {
+ ...options,
+ // Requested / rebuild closes own their follow-up; a self-close has no
+ // done event, so queued user input would otherwise wait forever.
+ wakeQueuedInput: context.closeReason === 'unexpected',
+ });
},
cleanupRuntimeState: () => cleanupClosedSessionRuntime(session),
});