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-desktop): 按传输进度延长剪贴板空闲期限

Dasha8225b6d8dcc
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/clipboardTransfer.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/clipboardTransfer.test.ts
new file mode 100644
index 0000000000..835a0fd780
--- /dev/null
+++ b/apps/desktop/src/main/remote-desktop/__tests__/clipboardTransfer.test.ts
@@ -0,0 +1,69 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { CLIPBOARD_CHUNK_CHARS, type RemoteClipboardContent } from '@cindy/device-link';
+import { ClipboardTransfer } from '../clipboardTransfer';
+
+beforeEach(() => vi.useFakeTimers());
+afterEach(() => vi.useRealTimers());
+
+describe('clipboard transfer idle expiry', () => {
+ it.each(['copy', 'paste'] as const)('keeps %s progressing beyond one minute and settles only once', async (direction) => {
+ const content = { text: 'x'.repeat(CLIPBOARD_CHUNK_CHARS * 2) };
+ const data = JSON.stringify(content);
+ const transfer = vi.fn(async (_action: 'copy' | 'paste', _content?: RemoteClipboardContent) => content);
+ const current = () => true;
+ const buffer = new ClipboardTransfer();
+ const common = { op: 'clipboardContent' as const, lease: 'lease' };
+ const { id } = await buffer.handle(
+ direction === 'copy' ? { ...common, action: 'copy' } : { ...common, action: 'begin', length: data.length },
+ current, transfer,
+ ) as { id: string };
+ let received = '';
+ for (let offset = 0; offset < data.length; offset += CLIPBOARD_CHUNK_CHARS) {
+ await vi.advanceTimersByTimeAsync(45_000);
+ const chunk = data.slice(offset, offset + CLIPBOARD_CHUNK_CHARS);
+ if (direction === 'copy') {
+ const reply = await buffer.handle({ ...common, action: 'read', id, offset }, current, transfer) as { data: string };
+ received += reply.data;
+ } else {
+ await buffer.handle({ ...common, action: 'write', id, offset, data: chunk }, current, transfer);
+ }
+ }
+ if (direction === 'copy') {
+ expect(received).toBe(data);
+ await buffer.handle({ ...common, action: 'cancel', id }, current, transfer);
+ } else {
+ await buffer.handle({ ...common, action: 'commit', id }, current, transfer);
+ expect(transfer).toHaveBeenCalledWith('paste', content, current);
+ await expect(buffer.handle({ ...common, action: 'commit', id }, current, transfer)).rejects.toThrow('CLIPBOARD_EXPIRED');
+ }
+ expect(transfer).toHaveBeenCalledTimes(1);
+ expect(vi.getTimerCount()).toBe(0);
+ });
+
+ it.each(['copy', 'paste'] as const)('expires idle %s even after invalid or unauthorized requests', async (direction) => {
+ const buffer = new ClipboardTransfer();
+ const transfer = vi.fn(async () => ({ text: 'content' }));
+ const common = { op: 'clipboardContent' as const, lease: 'lease' };
+ const { id } = await buffer.handle(
+ direction === 'copy' ? { ...common, action: 'copy' } : { ...common, action: 'begin', length: 100 },
+ () => true, transfer,
+ ) as { id: string };
+ const chunk = direction === 'copy'
+ ? { ...common, action: 'read' as const, id, offset: 0 }
+ : { ...common, action: 'write' as const, id, offset: 0, data: 'x' };
+ await vi.advanceTimersByTimeAsync(30_000);
+ await buffer.handle(chunk, () => true, transfer);
+ await vi.advanceTimersByTimeAsync(59_999);
+ await expect(buffer.handle({ ...chunk, id: 'wrong' }, () => true, transfer)).rejects.toThrow('CLIPBOARD_EXPIRED');
+ await expect(buffer.handle({ ...chunk, lease: 'other' }, () => true, transfer)).rejects.toThrow('CLIPBOARD_EXPIRED');
+ await expect(buffer.handle({ ...chunk, offset: 1000 }, () => true, transfer)).rejects.toThrow('INVALID_REQUEST');
+ const wrongDirection = direction === 'copy'
+ ? { ...common, action: 'write' as const, id, offset: 0, data: 'x' }
+ : { ...common, action: 'read' as const, id, offset: 0 };
+ await expect(buffer.handle(wrongDirection, () => true, transfer)).rejects.toThrow('INVALID_REQUEST');
+ await expect(buffer.handle(chunk, () => false, transfer)).rejects.toThrow('DESKTOP_LEASE_EXPIRED');
+ await vi.advanceTimersByTimeAsync(1);
+ await expect(buffer.handle(chunk, () => true, transfer)).rejects.toThrow('CLIPBOARD_EXPIRED');
+ expect(vi.getTimerCount()).toBe(0);
+ });
+});
diff --git a/apps/desktop/src/main/remote-desktop/clipboardTransfer.ts b/apps/desktop/src/main/remote-desktop/clipboardTransfer.ts
index d6d2cbdab5..bbd2d31d3a 100644
--- a/apps/desktop/src/main/remote-desktop/clipboardTransfer.ts
+++ b/apps/desktop/src/main/remote-desktop/clipboardTransfer.ts
@@ -4,7 +4,7 @@ import {
type ClipboardContentRequest, type RemoteClipboardContent,
} from '@cindy/device-link';
-/** One bounded, short-lived transfer per controller. Payloads never reach disk/logs. */
+/** One bounded transfer per controller, released after 60s idle. Payloads never reach disk/logs. */
export class ClipboardTransfer {
private value: { id: string; lease: string; length: number; data: string; direction: 'copy' | 'paste' } | null = null;
private timer: ReturnType<typeof setTimeout> | undefined;
@@ -37,12 +37,14 @@ export class ClipboardTransfer {
if (request.action === 'cancel') { this.reset(); return { ok: true }; }
if (request.action === 'read' && value.direction === 'copy') {
if (request.offset >= value.length) throw new Error('INVALID_REQUEST');
+ this.timer?.refresh(); // Slow links may need more than a minute for a complete item.
return { data: value.data.slice(request.offset, request.offset + CLIPBOARD_CHUNK_CHARS) };
}
if (request.action === 'write' && value.direction === 'paste') {
if (request.offset !== value.data.length || value.data.length + request.data.length > value.length)
throw new Error('INVALID_REQUEST');
value.data += request.data;
+ this.timer?.refresh();
return { ok: true };
}
if (request.action === 'commit' && value.direction === 'paste') {