Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
test(desktop): cover remote account-boundary cleanup failures
DavidShen158b798c8fec
diff --git a/apps/desktop/src/main/remote-agent/__tests__/connectionPool.test.ts b/apps/desktop/src/main/remote-agent/__tests__/connectionPool.test.tsindex be4c4e09d9..d2bbd5ef4b 100644--- a/apps/desktop/src/main/remote-agent/__tests__/connectionPool.test.ts+++ b/apps/desktop/src/main/remote-agent/__tests__/connectionPool.test.ts@@ -19,6 +19,8 @@ import {type PooledRemoteConnection,} from '../controller/connectionPool';import { hostRewritesResumeId, startRemoteAgentSession, type StartRemoteAgentDeps } from '../controller/startRemote';+import { ExecServerRelay } from '../controller/execServerRelay';+import { RemoteExecutor } from '../executor/executor';import { createRemoteAgentHost, type HostedStartInput } from '../host/runHost';import type { RemoteAgentLimits } from '../../../shared/remoteAgentLimits';@@ -613,6 +615,60 @@ afterEach(async () => {});describe('remote connection pool with a real host', () => {+ // #6921 unit: startRemoteAgentSession must attempt every local cleanup and keep+ // its failure for repeated teardown. Existing pool cases only failed remote close.+ // Minimal input: one live run, with each local disposer failing after releasing+ // its real resource. Reuses the loopback host harness without a real Agent process.+ it('attempts every local disposer and retains all failures across account-boundary teardown', async () => {+ const pool = newPool();+ const failures = ['pool', 'relay', 'mcp', 'executor'].map((name) => new Error(`${name} cleanup failed`));+ const remove = vi.fn();+ const add = pool.add.bind(pool);+ const addSpy = vi.spyOn(pool, 'add').mockImplementation((member) => {+ const leave = add(member);+ return () => { leave(); remove(); throw failures[0]; };+ });+ const closeRelay = ExecServerRelay.prototype.close;+ const relaySpy = vi.spyOn(ExecServerRelay.prototype, 'close').mockImplementation(function (this: ExecServerRelay) {+ closeRelay.call(this);+ throw failures[1];+ });+ const closeExecutor = RemoteExecutor.prototype.close;+ const executorSpy = vi.spyOn(RemoteExecutor.prototype, 'close').mockImplementation(async function (this: RemoteExecutor) {+ await closeExecutor.call(this);+ throw failures[3];+ });+ const disposeMcp = vi.fn(() => { throw failures[2]; });+ try {+ const handle = await startRemoteAgentSession('codex', {+ sessionId: randomUUID(), workingDir: project, model: 'm1',+ }, {+ ...deps(pool),+ // The fake Agent does not open exec-server; no Codex process is launched.+ codexPath: () => path.join(root, 'unused-codex'),+ prepareMcp: async () => ({ servers: new Map(), dispose: disposeMcp }),+ });+ opened.push(handle);+ const first = await Promise.allSettled([handle.close({ reason: 'account-boundary' })]);+ expect(first[0].status).toBe('rejected');+ if (first[0].status !== 'rejected') throw new Error('Local cleanup failure must reach the owner');+ expect(first[0].reason).toBeInstanceOf(AggregateError);+ expect((first[0].reason as AggregateError).errors).toEqual(failures);+ const repeated = await Promise.allSettled([handle.detach!({ reason: 'account-boundary' })]);+ expect(repeated[0].status).toBe('rejected');+ if (repeated[0].status === 'rejected') expect(repeated[0].reason).toBe(first[0].reason);+ expect(remove).toHaveBeenCalledOnce();+ expect(relaySpy).toHaveBeenCalledOnce();+ expect(disposeMcp).toHaveBeenCalledOnce();+ expect(executorSpy).toHaveBeenCalledOnce();+ expect(pool.size).toBe(0);+ } finally {+ addSpy.mockRestore();+ relaySpy.mockRestore();+ executorSpy.mockRestore();+ }+ });+// 回归:重启恢复遇到 1.5s 建连超时就失败;原用例只覆盖已连上的打开/连回。// 生产入口 startRemoteAgentSession,最小输入为 caps 连续两次 NOT_CONNECTED;// unit,假计时器验证等待期间另一控制端仍可请求,且恢复后只打开一个 run。diff --git a/apps/desktop/src/main/remote-agent/__tests__/plumbing.test.ts b/apps/desktop/src/main/remote-agent/__tests__/plumbing.test.tsindex cc1e8d8cde..90327b46fe 100644--- a/apps/desktop/src/main/remote-agent/__tests__/plumbing.test.ts+++ b/apps/desktop/src/main/remote-agent/__tests__/plumbing.test.ts@@ -353,6 +353,77 @@ describe('tunnel', () => {});describe('remote AgentSessionHandle close', () => {+ // #6921 unit: the production handle must distinguish remote notification failure+ // from local cleanup failure. The navigation-only test below missed this boundary.+ // Minimal inputs: a pending/rejected close and an onClosed cleanup still in flight.+ // Deferred promises exercise ordering without sleeps or real Agent processes.+ it.each(['close', 'detach'] as const)('releases local resources before account-boundary %s notification settles', async (mode) => {+ let failRemote!: (error: Error) => void;+ const remote = new Promise<void>((_resolve, reject) => { failRemote = reject; });+ const close = vi.fn(() => remote);+ const dispose = vi.fn(async () => undefined);+ const warn = vi.fn();+ const controller = createRemoteAgentHandle({+ client: { close } as unknown as RemoteAgentRunClient,+ started: { id: 'pi-1', agentKind: 'pi', model: 'm', shadowDir: '', methods: [], state: {} },+ workspace: new ExecutorWorkspace({ workingDir: root }),+ recordApproval: () => undefined, newId: () => 'req', dispose, log: { warn },+ });+ const closing = controller.handle[mode]!({ reason: 'account-boundary' });+ const outcome = Promise.allSettled([closing]);+ const remoteError = new Error('[DEVICE_LINK_NOT_CONNECTED] provider sharing is not ready');+ try {+ await new Promise<void>((resolve) => setImmediate(resolve));+ expect(close).toHaveBeenCalledWith(mode, 'account-boundary');+ expect(dispose).toHaveBeenCalledOnce();+ // Local event consumers can finish while the remote notification is pending.+ expect(await controller.handle.events()[Symbol.asyncIterator]().next()).toMatchObject({ done: true });+ failRemote(remoteError);+ expect(await outcome).toEqual([{ status: 'fulfilled', value: undefined }]);+ expect(warn).toHaveBeenCalledWith(expect.any(String), { error: String(remoteError) });+ } finally {+ failRemote(remoteError);+ await outcome;+ }+ });++ it.each(['resolve', 'reject'] as const)('shares onClosed cleanup and its %s result across close and detach', async (result) => {+ let finish!: () => void;+ let fail!: (error: Error) => void;+ const cleanup = new Promise<void>((resolve, reject) => { finish = resolve; fail = reject; });+ const dispose = vi.fn(() => cleanup);+ const controller = createRemoteAgentHandle({+ client: { close: async () => undefined } as unknown as RemoteAgentRunClient,+ started: { id: 'pi-1', agentKind: 'pi', model: 'm', shadowDir: '', methods: [], state: {} },+ workspace: new ExecutorWorkspace({ workingDir: root }),+ recordApproval: () => undefined, newId: () => 'req', dispose, log: { warn: vi.fn() },+ });+ controller.onClosed('closed');+ let settled = false;+ const pending = Promise.allSettled([+ controller.handle.close({ reason: 'account-boundary' }),+ controller.handle.detach!({ reason: 'account-boundary' }),+ ]).then((outcomes) => { settled = true; return outcomes; });+ const localError = new Error('local executor did not stop');+ try {+ // Let already-resolved remote calls settle; the local gate must still block both callers.+ await new Promise<void>((resolve) => setImmediate(resolve));+ expect(settled).toBe(false);+ expect(dispose).toHaveBeenCalledOnce();+ if (result === 'reject') fail(localError);+ else finish();+ const expected = result === 'reject'+ ? { status: 'rejected', reason: localError }+ : { status: 'fulfilled', value: undefined };+ expect(await pending).toEqual([expected, expected]);+ expect(await Promise.allSettled([controller.handle.close({ reason: 'account-boundary' })])).toEqual([expected]);+ expect(dispose).toHaveBeenCalledOnce();+ } finally {+ finish();+ await pending;+ }+ });+it('propagates a DEVICE_LINK close failure after disposing local resources', async () => {let disposed = 0;const client = {