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。

test: orca lead home switch must survive offline close

PraiseZhu27c27a5a7edd
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/sessionAgentSwitchHandler.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/sessionAgentSwitchHandler.test.ts
index 85a4da1384..e6cb0da933 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/sessionAgentSwitchHandler.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/sessionAgentSwitchHandler.test.ts
@@ -1470,6 +1470,66 @@ describe('远程 Agent:选模型时换 Agent 所在电脑', () => {
expect(leadHome.calls).toEqual([]);
});
+ it('协同 Lead 切回本机:关闭旧远程会话遇链路错误仍提交本机位置', async () => {
+ const linkError = Object.assign(
+ new Error('[REMOTE_AGENT_UNAVAILABLE] [DEVICE_LINK_DEVICE_OFFLINE] target offline'),
+ { code: 'DEVICE_OFFLINE' },
+ );
+ const applyNow = relocationHarness({ orcaRole: 'lead', agentDeviceId: 'office-pc' });
+ vi.mocked(applyNow.deps.closeSession).mockImplementationOnce(async () => {
+ applyNow.calls.push('close');
+ throw linkError;
+ });
+ await expect(
+ performSessionAgentSwitch(applyNow.deps, { ...backToTaskComputer, applyNow: true }),
+ ).resolves.toMatchObject({ switched: true });
+ expect(applyNow.deps.applyAgentSwitchToDb).toHaveBeenCalledWith(
+ 's1',
+ expect.objectContaining({ agentDeviceId: null }),
+ );
+ expect(applyNow.deps.bootstrapSwitchedSession).toHaveBeenCalledWith('s1');
+ expect(applyNow.deps.log.warn).toHaveBeenCalledWith(
+ expect.stringMatching(/returning|home|close/i),
+ expect.objectContaining({ sessionId: 's1', code: expect.any(String) }),
+ );
+ expect(applyNow.calls).toEqual(['close', 'db', 'provider', 'boundary', 'pending']);
+
+ const pending = relocationHarness({ orcaRole: 'lead', agentDeviceId: 'office-pc' });
+ await performSessionAgentSwitch(pending.deps, backToTaskComputer);
+ vi.mocked(pending.deps.closeSession).mockImplementationOnce(async () => {
+ pending.calls.push('close');
+ throw linkError;
+ });
+ await expect(applyPendingAgentSwitchIfIdle(pending.deps, 's1')).resolves.toBeUndefined();
+ expect(pending.deps.applyAgentSwitchToDb).toHaveBeenCalledWith(
+ 's1',
+ expect.objectContaining({ agentDeviceId: null }),
+ );
+ expect(pending.pending.get('s1')).toBeUndefined();
+ });
+
+ it('协同 Lead 切回本机:关闭失败非链路类仍中止且不写库', async () => {
+ const h = relocationHarness({ orcaRole: 'lead', agentDeviceId: 'office-pc' });
+ vi.mocked(h.deps.closeSession).mockImplementationOnce(async () => {
+ h.calls.push('close');
+ throw new Error('[REMOTE_AGENT_PEER_TOO_OLD] Cindy on the other computer is too old');
+ });
+ await expect(
+ performSessionAgentSwitch(h.deps, { ...backToTaskComputer, applyNow: true }),
+ ).rejects.toThrow(/REMOTE_AGENT_PEER_TOO_OLD/);
+ expect(h.deps.applyAgentSwitchToDb).not.toHaveBeenCalled();
+ expect(h.calls).toEqual(['close']);
+
+ const unavailable = relocationHarness({ orcaRole: 'lead', agentDeviceId: 'office-pc' });
+ vi.mocked(unavailable.deps.closeSession).mockRejectedValueOnce(
+ new Error('[REMOTE_AGENT_UNAVAILABLE] the agent did not start on that computer'),
+ );
+ await expect(
+ performSessionAgentSwitch(unavailable.deps, { ...backToTaskComputer, applyNow: true }),
+ ).rejects.toThrow(/REMOTE_AGENT_UNAVAILABLE/);
+ expect(unavailable.deps.applyAgentSwitchToDb).not.toHaveBeenCalled();
+ });
+
it('非法位置参数被拒绝', async () => {
const h = relocationHarness();
await expect(performSessionAgentSwitch(h.deps, { ...backToTaskComputer, agentDeviceId: 'bad id!' }))
diff --git a/apps/desktop/src/main/remote-agent/__tests__/plumbing.test.ts b/apps/desktop/src/main/remote-agent/__tests__/plumbing.test.ts
index 98f6dd1572..786f09aa88 100644
--- a/apps/desktop/src/main/remote-agent/__tests__/plumbing.test.ts
+++ b/apps/desktop/src/main/remote-agent/__tests__/plumbing.test.ts
@@ -125,6 +125,29 @@ describe('tunnel', () => {
});
});
+describe('remote AgentSessionHandle close', () => {
+ it('propagates a DEVICE_LINK close failure after disposing local resources', async () => {
+ let disposed = 0;
+ const client = {
+ async close() {
+ throw Object.assign(new Error('[REMOTE_AGENT_UNAVAILABLE] [DEVICE_LINK_DEVICE_OFFLINE] target offline'), {
+ code: 'DEVICE_OFFLINE',
+ });
+ },
+ } as unknown as RemoteAgentRunClient;
+ const controller = createRemoteAgentHandle({
+ client,
+ started: { id: 'sdk-1', agentKind: 'claude-code', model: 'm', shadowDir: '', methods: [], state: {} },
+ workspace: new ExecutorWorkspace({ workingDir: root }),
+ recordApproval: () => undefined,
+ newId: () => 'req',
+ dispose: async () => { disposed += 1; },
+ });
+ await expect(controller.handle.close({ reason: 'navigation' })).rejects.toThrow(/DEVICE_LINK_DEVICE_OFFLINE/);
+ expect(disposed).toBe(1);
+ });
+});
+
describe('confirmations raised on this computer', () => {
it('uses the Agent confirmation channel and offers an explicit credential grant for this task', async () => {
let next = 0;
diff --git a/apps/desktop/src/main/remote-agent/__tests__/runClientClose.test.ts b/apps/desktop/src/main/remote-agent/__tests__/runClientClose.test.ts
new file mode 100644
index 0000000000..fb70bbdd62
--- /dev/null
+++ b/apps/desktop/src/main/remote-agent/__tests__/runClientClose.test.ts
@@ -0,0 +1,34 @@
+import { describe, expect, it, vi } from 'vitest';
+
+import type { RemoteAgentPoller } from '../controller/poller';
+import { RemoteAgentRunClient } from '../controller/runClient';
+
+function handlers() {
+ return {
+ onEvent: () => undefined,
+ onState: () => undefined,
+ onWs: () => undefined,
+ onClosed: vi.fn(),
+ onRequest: async () => ({ type: 'interaction' as const, result: { kind: 'permission' as const, behavior: 'allow' as const } }),
+ };
+}
+
+describe('RemoteAgentRunClient.close', () => {
+ it('rejects a DEVICE_LINK close after locally finishing the client', async () => {
+ const invoke = vi.fn(async () => {
+ throw Object.assign(new Error('[DEVICE_LINK_NOT_CONNECTED] not connected'), { code: 'NOT_CONNECTED' });
+ });
+ const seen = handlers();
+ const client = new RemoteAgentRunClient(
+ 'run-1',
+ { invoke, unregister: () => undefined } as unknown as RemoteAgentPoller,
+ seen,
+ () => 'id',
+ );
+ await expect(client.close('close', 'navigation')).rejects.toMatchObject({
+ message: expect.stringMatching(/DEVICE_LINK_NOT_CONNECTED/),
+ });
+ expect(client.isClosed).toBe(true);
+ expect(seen.onClosed).toHaveBeenCalledWith('closed', undefined);
+ });
+});