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): close 没送到的占位不随任务注销消失

fmfsaisai2557fdf0e678
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 188aa16985..8f91938ccd 100644
--- a/apps/desktop/src/main/remote-agent/__tests__/connectionPool.test.ts
+++ b/apps/desktop/src/main/remote-agent/__tests__/connectionPool.test.ts
@@ -233,6 +233,25 @@ describe('remote connection pool', () => {
}
});
+ // PR #6483 第四轮:占位随任务注销一起消失,对方的旧 run 却还占着名额。
+ it('keeps a held old run counted after its task leaves the pool, until the provider reclaims it', async () => {
+ vi.useFakeTimers();
+ try {
+ const pool = new RemoteConnectionPool({ idleMinutes: () => 10, now: () => now, checkMs: 60 * MINUTE, closeRetryMs: 2_000, closeHoldMs: 3 * MINUTE });
+ const stuck = member('stuck', now - 60 * MINUTE);
+ stuck.occupied = false;
+ const leave = pool.add(stuck);
+ pool.holdAfterFailedClose(stuck, async () => { throw new Error('link down'); });
+ leave();
+ expect(pool.occupied()).toBe(1);
+ await vi.advanceTimersByTimeAsync(3 * MINUTE);
+ expect(pool.occupied()).toBe(0);
+ expect(vi.getTimerCount()).toBe(0);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it('checks the watermark periodically while connections are registered', async () => {
vi.useFakeTimers();
try {
@@ -991,6 +1010,34 @@ describe('remote connection pool with a real host', () => {
}
});
+ it('still counts the old run of a task closed while its release close was undelivered, then drops it once closed', async () => {
+ limits = { perDevice: 2, total: 64 };
+ const pool = new RemoteConnectionPool({ idleMinutes: () => idleMinutes, checkMs: 3_600_000, closeRetryMs: 50 });
+ const a = await start(pool);
+ await ask(a, watch(a), 'hello');
+ await sleep(5);
+ const b = await start(pool);
+ await ask(b, watch(b), 'hello');
+ let dropped = false;
+ intercept = (request) => {
+ if (request.op !== 'close' || dropped) return undefined;
+ dropped = true;
+ throw new Error('[DEVICE_LINK_TIMEOUT] link down');
+ };
+ expect(await pool.releaseForRetry(member('other', 0, false))).toBe(true);
+ // 占位期间关掉任务 a:对方的旧 run 还在。
+ await a.close({ reason: 'navigation' });
+ expect(host.limitUsage().total).toBe(2);
+ await sleep(5);
+ opens = 0;
+ await start(pool);
+ // 仍按 a 的旧 run 占着算:打开前先让了 b,只开一次,没有撞上限。
+ expect(opens).toBe(1);
+ // 重发的 close 送到后,对方结束 a 的旧 run,本机也不再占位。
+ await vi.waitFor(() => expect(host.limitUsage().total).toBe(1));
+ await vi.waitFor(() => expect(pool.occupied()).toBe(1));
+ });
+
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/connectionPool.ts b/apps/desktop/src/main/remote-agent/controller/connectionPool.ts
index 11716d0710..9d06728b4e 100644
--- a/apps/desktop/src/main/remote-agent/controller/connectionPool.ts
+++ b/apps/desktop/src/main/remote-agent/controller/connectionPool.ts
@@ -60,7 +60,10 @@ export interface RemoteConnectionPoolOptions {
export class RemoteConnectionPool {
private readonly members = new Set<PooledRemoteConnection>();
- /** 让出时 close 没送到、对方可能还占着名额的连接 → 结束占位、再发一次 close。 */
+ /**
+ * 让出时 close 没送到、对方可能还占着名额的旧 run → 结束占位、再发一次 close。占位独立于任务存在:
+ * 任务关闭或归档(注销)后照样占着,直到再发成功或过了对方的回收期。
+ */
private readonly held = new Map<PooledRemoteConnection, { end: () => void; retryClose: () => Promise<unknown> }>();
private maxRuns: number | null = null;
private timer: ReturnType<typeof setInterval> | null = null;
@@ -80,7 +83,6 @@ export class RemoteConnectionPool {
(this.timer as { unref?: () => void }).unref?.();
}
return () => {
- this.held.get(member)?.end();
this.members.delete(member);
if (this.members.size === 0 && this.timer) {
clearInterval(this.timer);
@@ -103,11 +105,9 @@ export class RemoteConnectionPool {
* 结束的旧 run 照样算(对方要到新进程启动时才替换同一任务的旧实例,打开时的名额检查会算上它)。
*/
occupied(except?: PooledRemoteConnection): number {
- let count = 0;
- for (const member of this.members) {
- if (member !== except && member.occupiesSlot()) count += 1;
- if (this.held.has(member)) count += 1;
- }
+ // 占位中的旧 run 都算,包括任务已注销的。
+ let count = this.held.size;
+ for (const member of this.members) if (member !== except && member.occupiesSlot()) count += 1;
return count;
}
diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.md
index 954f2faef5..e3f4382a8a 100644
--- a/docs/dev-rules/protocol-compatibility.md
+++ b/docs/dev-rules/protocol-compatibility.md
@@ -1377,7 +1377,7 @@ Codex 原生图片恢复(#6412)在既有 `maker:remote-agent:v1` 上追加
退出 App(`detach`)只收本机部分,不再联系 B。
- **让出时 close 没送到**(链路抖动等):B 的进程可能还占着名额。A 的连接池继续把它算作占位,约 2 秒后原样再发一次
`close`(B 不认识这个 run 时直接回空,等同已关闭);再发送到、或过了 B 自行回收的时间(`REMOTE_AGENT_HOST_IDLE_MS`,
- 3 分钟)后不再占位。占位期间水位线与打开前的判断都算上它,会先让别的有资格的连接,而不是直接打开撞上限。
+ 3 分钟)后不再占位;占位独立于任务存在,任务在这期间关闭或归档也不会让它消失。占位期间水位线与打开前的判断都算上它,会先让别的有资格的连接,而不是直接打开撞上限。
close 没送到时本机也不再等 B 的收尾事件(B 不会收尾)。这个任务自己要连回时,打开前与被拒后都先立刻对旧 run 再发
一次 close,关掉了才不再占位;计数时只排除它正要开的那个位置,旧 run 照样算(B 打开时的名额检查会算上旧 run,
同一任务的旧实例要到新进程启动时才被替换)。还是关不掉就照常让别的连接或按 B 的上限报错。