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): reply 重试复用载荷;任务收尾清理影子工作区

MagicLizi1b444da99601
diff --git a/apps/desktop/src/main/remote-agent/__tests__/runClientReply.test.ts b/apps/desktop/src/main/remote-agent/__tests__/runClientReply.test.ts
index 909366f77e..b8dd0707f3 100644
--- a/apps/desktop/src/main/remote-agent/__tests__/runClientReply.test.ts
+++ b/apps/desktop/src/main/remote-agent/__tests__/runClientReply.test.ts
@@ -10,11 +10,13 @@ import { RemoteAgentRunClient } from '../controller/runClient';
describe('RemoteAgentRunClient reply delivery', () => {
it('retries a reply through transient link failures so the other side does not stall', async () => {
const replyAttempts: number[] = [];
+ const replyPayloads: unknown[] = [];
let failuresLeft = 2;
const invoke = async (args: unknown[]): Promise<unknown> => {
const op = (args[0] as { op: string }).op;
if (op === 'reply') {
replyAttempts.push(replyAttempts.length + 1);
+ replyPayloads.push((args[0] as { payload: unknown }).payload);
if (failuresLeft > 0) {
failuresLeft -= 1;
throw new Error('TIMEOUT: link dropped mid-reply');
@@ -49,5 +51,7 @@ describe('RemoteAgentRunClient reply delivery', () => {
}
expect(replyAttempts).toHaveLength(3);
expect(failuresLeft).toBe(0);
+ // 重试复用同一份载荷(同一引用):重建载荷会在对方 staging 里多留一份无人消费的上传。
+ expect(new Set(replyPayloads).size).toBe(1);
});
});
diff --git a/apps/desktop/src/main/remote-agent/controller/runClient.ts b/apps/desktop/src/main/remote-agent/controller/runClient.ts
index 7f09148d6b..330c27e204 100644
--- a/apps/desktop/src/main/remote-agent/controller/runClient.ts
+++ b/apps/desktop/src/main/remote-agent/controller/runClient.ts
@@ -277,13 +277,18 @@ export class RemoteAgentRunClient implements PolledRun {
* 回包交付:链路抖动导致发送失败时按退避重试。对方收到 reply 才会结束对该 requestId
* 的等待,只记日志不重试会让任务一直停在权限确认 / 工具请求上;对方按 id 去重,
* 重复交付无副作用。任务结束或非链路类错误(载荷过大等)才放弃。
+ *
+ * 载荷只建一次、重试复用:重试重建会再走一遍上传,而对方对重复 reply 在 pending
+ * 查询处就返回、不会消费新上传,白白占住对方的 staging 缓冲;若首份上传已被消费、
+ * 只是响应丢了,复用同一载荷还能直接命中幂等返回。
*/
private async deliverReply(requestId: string, reply: RemoteAgentReply): Promise<void> {
let delay = 500;
+ let payload: RemoteAgentPayload | undefined;
for (let attempt = 1; ; attempt += 1) {
if (this.closed) return;
try {
- const payload = await this.payload(reply);
+ if (!payload) payload = await this.payload(reply);
await this.invoke([{ op: 'reply', runId: this.runId, requestId, payload }]);
return;
} catch (error) {
diff --git a/apps/desktop/src/main/remote-agent/host/runHost.ts b/apps/desktop/src/main/remote-agent/host/runHost.ts
index 9f29091cce..f5b7d2d74b 100644
--- a/apps/desktop/src/main/remote-agent/host/runHost.ts
+++ b/apps/desktop/src/main/remote-agent/host/runHost.ts
@@ -128,6 +128,8 @@ interface Run {
handle?: AgentSessionHandle;
tunnel?: RunTunnel;
attachmentsDir: string;
+ /** 影子工作区根(按 hostSessionId 复用,任务收尾后清理)。 */
+ workspaceDir?: string;
pending: Map<string, PendingReverse>;
calls: Map<string, 'running' | 'done'>;
pushSeq: Set<number>;
@@ -401,7 +403,7 @@ export function createRemoteAgentHost(deps: RemoteAgentHostDeps) {
controller: string,
payload: RemoteAgentOpenPayload,
hostSessionId: string,
- ): Promise<{ shadowDir: string; mirrorRoot: string }> {
+ ): Promise<{ shadowDir: string; mirrorRoot: string; sessionRoot: string }> {
const controllerDir = createHash('sha256').update(controller).digest('hex').slice(0, 16);
const sessionRoot = path.join(deps.runsRoot, 'workspaces', controllerDir, hostSessionId);
const mirrorRoot = path.join(sessionRoot, 'fs');
@@ -438,14 +440,14 @@ export function createRemoteAgentHost(deps: RemoteAgentHostDeps) {
await writeNew(target, Buffer.from(file.data, 'base64'));
}
if (personal.permissions) await mergeLocalPermissions(path.join(shadowDir, '.claude', 'settings.local.json'), personal.permissions);
- return { shadowDir, mirrorRoot };
+ return { shadowDir, mirrorRoot, sessionRoot };
}
async function startRun(run: Run, payload: RemoteAgentOpenPayload): Promise<void> {
try {
const hostSessionId = hostSessionIdFor(run.controller, payload.sessionId);
run.hostSessionId = hostSessionId;
- const { shadowDir, mirrorRoot } = await withShadowLock(hostSessionId, async () => {
+ const { shadowDir, mirrorRoot, sessionRoot } = await withShadowLock(hostSessionId, async () => {
// 断线后旧实例还没清理就重新打开同一任务时,新旧实例共用同一个影子目录:
// 先结束旧实例再重建,避免删掉旧 Agent 还在使用的目录,或两个实例争用重建后的目录。
for (const other of runs.values()) {
@@ -455,6 +457,7 @@ export function createRemoteAgentHost(deps: RemoteAgentHostDeps) {
}
return prepareShadow(run.controller, payload, hostSessionId);
});
+ run.workspaceDir = sessionRoot;
const tunnel = await createRunTunnel({
http: (request, signal) => reverseHttp(run, request, signal),
wsOpen: (connId, wsPath) => append(run, { t: 'ws', connId, kind: 'open', path: wsPath }),
@@ -550,6 +553,12 @@ export function createRemoteAgentHost(deps: RemoteAgentHostDeps) {
async function disposeRun(run: Run): Promise<void> {
runs.delete(key(run.controller, run.id));
await fsp.rm(run.attachmentsDir, { recursive: true, force: true }).catch(() => undefined);
+ // 影子工作区(项目说明类文件的副本)在任务收尾后一并清理,不长期留在本机;同一任务的
+ // 新旧实例共用同一份目录,只有没有其它实例还在用时才删,避免删掉新实例正在用的目录。
+ if (run.workspaceDir && run.hostSessionId !== undefined
+ && ![...runs.values()].some((other) => other.hostSessionId === run.hostSessionId)) {
+ await fsp.rm(run.workspaceDir, { recursive: true, force: true }).catch(() => undefined);
+ }
stopSweepIfIdle();
}