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): 连回后补发设置完成前,其它调用都等这次连回

fmfsaisaid5a2318734ce
diff --git a/apps/desktop/src/main/remote-agent/__tests__/connectionPool.test.ts b/apps/desktop/src/main/remote-agent/__tests__/connectionPool.test.ts
index 7837b4ff58..b492316561 100644
--- a/apps/desktop/src/main/remote-agent/__tests__/connectionPool.test.ts
+++ b/apps/desktop/src/main/remote-agent/__tests__/connectionPool.test.ts
@@ -776,6 +776,56 @@ describe('remote connection pool with a real host', () => {
expect(scenario.calls.filter((call) => call.method === 'send')).toHaveLength(3);
});
+ // PR #6483 review:连回后恢复设置(reapply)完成前就算连上了,并发的第二条消息会抢在恢复前、甚至第一条之前发给对方。
+ it('a second send during the post-reconnect restore waits for it and goes after the first', async () => {
+ const pool = newPool();
+ const a = await start(pool);
+ const events = watch(a);
+ await ask(a, events, 'hello');
+ await a.setExtraDirs!([], path.join(root, 'library'));
+ await sleep(5);
+ await start(pool);
+ await sleep(5);
+ // 连回后补发 libraryRoot(setExtraDirs)时卡住。
+ let restoreGate: (() => void) | null = null;
+ intercept = (request) => {
+ if (restoreGate || request.op !== 'call' || request.method !== 'setExtraDirs') return undefined;
+ return new Promise((resolve) => {
+ restoreGate = () => resolve(host.handle(CONTROLLER, request).then((value) => value ?? null));
+ });
+ };
+ const before = scenario.calls.length;
+ const first = a.send({ type: 'user', content: 'one' });
+ await vi.waitFor(() => expect(restoreGate).not.toBeNull());
+ const second = a.send({ type: 'user', content: 'two' });
+ await sleep(5);
+ expect(scenario.calls.slice(before).filter((call) => call.method === 'send')).toEqual([]);
+ restoreGate!();
+ await Promise.all([first, second]);
+ await vi.waitFor(() => expect(scenario.calls.slice(before).map((call) => call.method === 'send'
+ ? `send:${(call.args[0] as { content: string }).content}`
+ : call.method)).toEqual(['setExtraDirs', 'send:one', 'send:two']));
+ expect(scenario.started).toHaveLength(3);
+ });
+
+ it('after a failed reconnect the next send tries again instead of reusing the failed attempt', async () => {
+ limits = { perDevice: 16, total: 64 };
+ const pool = newPool();
+ const a = await start(pool);
+ const events = watch(a);
+ await ask(a, events, 'hello');
+ await sleep(5);
+ expect(await pool.releaseForRetry(member('other', 0, false))).toBe(true);
+ intercept = (request) => {
+ if (request.op !== 'open') return undefined;
+ intercept = null;
+ throw new Error('[REMOTE_AGENT_UNAVAILABLE] offline');
+ };
+ await expect(a.send({ type: 'user', content: 'lost' })).rejects.toThrow('offline');
+ expect(await ask(a, events, 'again')).toBe('ok:again');
+ expect(scenario.started).toHaveLength(2);
+ });
+
it('gives up a release once a send has started', async () => {
const pool = newPool();
const a = await start(pool);
diff --git a/apps/desktop/src/main/remote-agent/controller/startRemote.ts b/apps/desktop/src/main/remote-agent/controller/startRemote.ts
index 2df7d465ea..5f6120e625 100644
--- a/apps/desktop/src/main/remote-agent/controller/startRemote.ts
+++ b/apps/desktop/src/main/remote-agent/controller/startRemote.ts
@@ -683,17 +683,21 @@ export async function startRemoteAgentSession(
}
}
- /** 要对方 Agent 干活时:已让出名额就连回同一个会话。正在让就等它让完;同时只连一次。 */
+ /**
+ * 要对方 Agent 干活时:已让出名额就连回同一个会话。正在让就等它让完;同时只连一次。
+ * 连回期间(含连回后补发设置)其它调用都等它:打开成功时状态已是 connected,不能抢在设置恢复前用这条连接。
+ */
async function ensureConnected(): Promise<RemoteAgentRunClient> {
for (;;) {
if (linkState === 'closed') throw new Error('[REMOTE_AGENT_EXPIRED] The task has ended on this computer.');
- if (linkState === 'connected' && active) return active;
if (reconnecting) return reconnecting;
+ if (linkState === 'connected' && active) return active;
if (!releasing) break;
await releasing.catch(() => undefined);
}
linkState = 'connecting';
- const attempt = (async () => {
+ let attempt: Promise<RemoteAgentRunClient> | null = null;
+ attempt = (async () => {
try {
const client = await connect(true, (started) => {
controller?.onReconnected(started);
@@ -707,14 +711,13 @@ export async function startRemoteAgentSession(
// 连不回(对方忙、离线等):保持已让出名额,下次发送再试。
if (stateNow() === 'connecting') linkState = 'released';
throw localizeRemoteError(error);
+ } finally {
+ // 在这次尝试结束的同时清掉:之后来的调用不会拿到已经失败的旧尝试。
+ if (reconnecting === attempt) reconnecting = null;
}
})();
reconnecting = attempt;
- try {
- return await attempt;
- } finally {
- if (reconnecting === attempt) reconnecting = null;
- }
+ return attempt;
}
const link: RemoteAgentLink = {