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(remote-agent): 本机这侧断开 exec-server 连接时也报告由忙转闲

fmfsaisaic33dcdd17fa9
diff --git a/apps/desktop/src/main/remote-agent/__tests__/execServerProjection.e2e.test.ts b/apps/desktop/src/main/remote-agent/__tests__/execServerProjection.e2e.test.ts
index 7909c979f7..788bf8ded4 100644
--- a/apps/desktop/src/main/remote-agent/__tests__/execServerProjection.e2e.test.ts
+++ b/apps/desktop/src/main/remote-agent/__tests__/execServerProjection.e2e.test.ts
@@ -90,6 +90,8 @@ describe.skipIf(!fs.existsSync(CODEX) || process.platform === 'win32')('exec-ser
const messages: Array<{ id?: number; method?: string; params?: { processId?: string }; error?: unknown }> = [];
let parts = '';
let ended = 0;
+ // 模拟交付链路断开:下一次推送失败,中继会丢掉未发帧并从本机这侧关掉全部连接。
+ let failNextPush = false;
const relay = new ExecServerRelay({
codexPath: CODEX,
cwd: root,
@@ -100,6 +102,10 @@ describe.skipIf(!fs.existsSync(CODEX) || process.platform === 'win32')('exec-ser
ended += 1;
},
push: async (frames) => {
+ if (failNextPush) {
+ failNextPush = false;
+ throw new Error('link down');
+ }
for (const frame of frames) {
if (frame.kind !== 'message') continue;
parts += frame.data;
@@ -141,13 +147,26 @@ describe.skipIf(!fs.existsSync(CODEX) || process.platform === 'win32')('exec-ser
// closed / exited 可能都会来,只算一次。
expect(ended).toBe(2);
- // 连接带着还在跑的进程被整条关掉(对方关闭 WebSocket、exec-server 异常退出),同样算一次由忙转闲。
- start(6, 'dropped', 'sleep 30');
+ // 本机这侧断开(推送失败、exec-server 异常退出):先排进关闭帧再删连接,带着的工作同样算一次由忙转闲。
+ // 用对方不认识的方法只换来一条错误回复,进程仍在跑,不会经 process/closed 先报一次。
+ start(6, 'local', 'sleep 30');
await expect.poll(() => messages.some((item) => item.id === 6), { timeout: 5000 }).toBe(true);
expect(relay.busy()).toBe(true);
+ failNextPush = true;
+ send({ id: 7, method: 'cindy/test-ping', params: {} });
+ await expect.poll(() => ended, { timeout: 5000 }).toBe(3);
+
+ // 对方关闭 WebSocket:连接带着还在跑的进程被整条删掉,同样算一次。
+ relay.handle({ t: 'ws', connId: 'c1', kind: 'open', path: '/ws/exec-server' });
+ send({ id: 8, method: 'initialize', params: { clientName: 'cindy-test', clientVersion: '1' } });
+ await expect.poll(() => messages.find((item) => item.id === 8), { timeout: 5000 }).toBeDefined();
+ send({ method: 'initialized', params: {} });
+ start(10, 'dropped', 'sleep 30');
+ await expect.poll(() => messages.some((item) => item.id === 10), { timeout: 5000 }).toBe(true);
+ expect(relay.busy()).toBe(true);
relay.handle({ t: 'ws', connId: 'c1', kind: 'close' });
await expect.poll(() => relay.busy(), { timeout: 5000 }).toBe(false);
- expect(ended).toBe(3);
+ expect(ended).toBe(4);
// 让出名额后关掉 exec-server,中继本身还能接新的连接。
relay.reset();
diff --git a/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts b/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts
index dcf554b795..82d5ca4584 100644
--- a/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts
+++ b/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts
@@ -362,7 +362,11 @@ export class ExecServerRelay {
* 这时断开对方的 Agent 会丢掉它们,连接池不让出名额。
*/
busy(): boolean {
- if (this.outbox.length || this.flushing) return true;
+ return this.outbox.length > 0 || Boolean(this.flushing) || this.hasWork();
+ }
+
+ /** 各条连接上还有没有工作(在跑的进程、启动中的请求、待确认的消息);还没推完的帧不算。 */
+ private hasWork(): boolean {
for (const connection of this.connections.values()) {
if (connection.processes.size || connection.starting.size || connection.queue) return true;
}
@@ -549,7 +553,8 @@ export class ExecServerRelay {
private dropConnection(connId: string, notifyRemote: boolean): void {
const connection = this.connections.get(connId);
if (!connection || connection.closed) return;
- // 连接带着在跑的进程、启动中的请求或待确认的消息被整条删掉(异常退出、对方关闭),同样是一次由忙转闲。
+ // 连接带着在跑的进程、启动中的请求或待确认的消息被整条删掉(异常退出、交付失败、对方关闭),同样是一次
+ // 由忙转闲。只看各连接上的工作:本机这侧断开时紧接着排进去的关闭帧不算忙。
const hadWork = connection.processes.size > 0 || connection.starting.size > 0 || Boolean(connection.queue);
connection.closed = true;
this.connections.delete(connId);
@@ -560,7 +565,7 @@ export class ExecServerRelay {
// 进程已退出。
}
if (notifyRemote) this.send({ connId, kind: 'close' });
- if (hadWork && !this.busy()) this.deps.onProcessesEnded?.();
+ if (hadWork && !this.hasWork()) this.deps.onProcessesEnded?.();
}
/** 一条消息:超过单帧上限时拆成多段(除最后一段外带 more)。 */