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 中继统一按工作由有变无报告由忙转闲

fmfsaisai8eb67ef4e678
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 788bf8ded4..c0662cbc3f 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
@@ -174,6 +174,16 @@ describe.skipIf(!fs.existsSync(CODEX) || process.platform === 'win32')('exec-ser
relay.handle({ t: 'ws', connId: 'c2', kind: 'open', path: '/ws/exec-server' });
relay.handle({ t: 'ws', connId: 'c2', kind: 'message', data: JSON.stringify({ id: 9, method: 'initialize', params: { clientName: 'cindy-test', clientVersion: '1' } }) });
await expect.poll(() => messages.find((item) => item.id === 9), { timeout: 5000 }).toBeDefined();
+
+ // 启动失败(exec-server 回 JSON-RPC 错误)不会留下进程,同样算一次由忙转闲。
+ const sendC2 = (message: unknown) => relay.handle({ t: 'ws', connId: 'c2', kind: 'message', data: JSON.stringify(message) });
+ sendC2({ method: 'initialized', params: {} });
+ sendC2({ id: 11, method: 'process/start', params: {
+ processId: 'missing-binary', argv: [path.join(root, 'no-such-binary')], cwd: pathToFileURL(root).href, env: {}, tty: false,
+ } });
+ await expect.poll(() => messages.find((item) => item.id === 11), { timeout: 5000 }).toBeDefined();
+ expect(messages.find((item) => item.id === 11)?.error).toBeDefined();
+ await expect.poll(() => ended, { timeout: 5000 }).toBe(5);
} finally {
relay.close();
fs.rmSync(root, { recursive: true, force: true });
diff --git a/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts b/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts
index 82d5ca4584..8bb039b0e7 100644
--- a/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts
+++ b/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts
@@ -47,7 +47,8 @@ export interface ExecServerRelayDeps {
log?: { warn(message: string, meta?: Record<string, unknown>): void };
now?: () => number;
/**
- * 由忙转闲:最后一个在跑的进程(含后台终端)结束,或带着工作的连接被整条关掉。对连接池来说这是一次活动。
+ * 由忙转闲:各连接上的工作(在跑的进程含后台终端、启动中的请求、待确认的消息)由有变无,包括进程结束、
+ * 启动失败、确认处理完、带着工作的连接被整条关掉。对连接池来说这是一次活动。
*/
onProcessesEnded?(): void;
}
@@ -488,7 +489,10 @@ export class ExecServerRelay {
});
connection.queue = next;
void next.then(() => {
- if (connection.queue === next) connection.queue = undefined;
+ if (connection.queue !== next) return;
+ const hadWork = this.hasWork();
+ connection.queue = undefined;
+ this.settleWork(hadWork);
});
}
@@ -520,18 +524,30 @@ export class ExecServerRelay {
const key = JSON.stringify(message.id);
const processId = connection.starting.get(key);
if (processId === undefined) return;
+ const hadWork = this.hasWork();
connection.starting.delete(key);
+ // 启动失败(JSON-RPC 错误)不会留下进程;等了很久才失败时同样是一次由忙转闲。
if (message.error === undefined) connection.processes.add(processId);
+ this.settleWork(hadWork);
return;
}
if ((message.method === 'process/closed' || message.method === 'process/exited') && typeof message.params?.processId === 'string') {
const processId = message.params.processId;
- const ended = connection.processes.delete(processId);
+ const hadWork = this.hasWork();
+ connection.processes.delete(processId);
for (const [key, starting] of connection.starting) if (starting === processId) connection.starting.delete(key);
- if (ended && ![...this.connections.values()].some((item) => item.processes.size)) this.deps.onProcessesEnded?.();
+ this.settleWork(hadWork);
}
}
+ /**
+ * 改动连接上的工作记录之后调用:由有工作变为没有工作,就是一次由忙转闲(对连接池来说是一次活动)。
+ * 进程结束、启动失败、待确认的消息处理完、连接被整条删掉,都经过这里。
+ */
+ private settleWork(hadWork: boolean): void {
+ if (hadWork && !this.hasWork()) this.deps.onProcessesEnded?.();
+ }
+
private reject(connId: string, id: unknown, decision: ExecutorGateDecision): void {
this.counts.rejected += 1;
// 被闸门拦下的启动没有到 exec-server,不会有进程。
@@ -555,7 +571,7 @@ export class ExecServerRelay {
if (!connection || connection.closed) return;
// 连接带着在跑的进程、启动中的请求或待确认的消息被整条删掉(异常退出、交付失败、对方关闭),同样是一次
// 由忙转闲。只看各连接上的工作:本机这侧断开时紧接着排进去的关闭帧不算忙。
- const hadWork = connection.processes.size > 0 || connection.starting.size > 0 || Boolean(connection.queue);
+ const hadWork = this.hasWork();
connection.closed = true;
this.connections.delete(connId);
try {
@@ -565,7 +581,7 @@ export class ExecServerRelay {
// 进程已退出。
}
if (notifyRemote) this.send({ connId, kind: 'close' });
- if (hadWork && !this.hasWork()) this.deps.onProcessesEnded?.();
+ this.settleWork(hadWork);
}
/** 一条消息:超过单帧上限时拆成多段(除最后一段外带 more)。 */
diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.md
index 71be1f9be8..7a8b10a4ab 100644
--- a/docs/dev-rules/protocol-compatibility.md
+++ b/docs/dev-rules/protocol-compatibility.md
@@ -1350,7 +1350,8 @@ Codex 原生图片恢复(#6412)在既有 `maker:remote-agent:v1` 上追加
Claude Code 后台子代理与后台任务、Pi 子代理)与待唤醒续跑(`pendingWake`);Codex 中继里没有仍在运行的 `process/start` 进程
(后台终端;按 exec-server 的 `process/closed` / `process/exited` 与启动失败判断,固定 Codex 0.159.2 实测),也没有在等本机确认的消息;
回到某条消息后、下一轮跑完前也算忙(Claude Code 要到下一次发送才在 B 上截断)。A 本机执行器里的后台命令不算忙。「闲置满 N 分钟」从最后一次活动算起:由忙转闲(后台任务、待唤醒续跑清空,
- Codex 中继里最后一个进程结束,或带着进程、启动中请求、待确认消息的 exec-server 连接被整条关掉)本身算一次活动,后台工作再久,结束后也要再闲置满 N 分钟才让。
+ Codex 中继里各连接上的工作——在跑的进程、启动中请求、待确认消息——由有变无:进程结束、启动失败、确认处理完,
+ 或带着工作的 exec-server 连接被整条关掉)本身算一次活动,后台工作再久,结束后也要再闲置满 N 分钟才让。
会话 id 会被 B 改写的也不让:B 打开时把启动选项里的字符串按 A 的工作目录及各级上级目录、用户目录与附加目录投影到虚拟
工作区,`resumeSessionId` 也不例外;Pi 的会话 id 是 B 上的会话文件路径,两台同为 Mac(`/Users`)或同为 Windows 时
常落在这些前缀下,带过去会变成不存在的文件。A 按同一规则预判(`hostRewritesResumeId`),这样的 Pi 任务不让出。