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): 保留每次跳过提示并排除空内容冲突

Dashbff11f749e8e
diff --git a/apps/desktop/src/main/remote-desktop-viewer/__tests__/safety.test.ts b/apps/desktop/src/main/remote-desktop-viewer/__tests__/safety.test.ts
index 8157acc787..f1a1b29578 100644
--- a/apps/desktop/src/main/remote-desktop-viewer/__tests__/safety.test.ts
+++ b/apps/desktop/src/main/remote-desktop-viewer/__tests__/safety.test.ts
@@ -36,7 +36,17 @@ it.each([
expect((await safety.tick(options)).notice).toBeNull();
version = '2';
expect((await safety.tick(options)).notice).toBe(notice);
+ const first = safety.snapshot().clipboardNoticeVersion;
+ expect(first).toBeGreaterThan(0);
+ expect((await safety.tick(options)).clipboardNoticeVersion).toBe(first);
version = '3';
+ const repeated = await safety.tick(options);
+ expect(repeated.notice).toBe(notice);
+ expect(repeated.clipboardNoticeVersion).toBe(first! + 1);
+ expect((await safety.tick(options)).clipboardNoticeVersion).toBe(
+ repeated.clipboardNoticeVersion,
+ );
+ version = '4';
error = 'CLIPBOARD_EMPTY';
expect((await safety.tick(options)).notice).toBeNull();
},
diff --git a/apps/desktop/src/main/remote-desktop-viewer/safety.ts b/apps/desktop/src/main/remote-desktop-viewer/safety.ts
index 4ff10d63ea..2ba5aa0707 100644
--- a/apps/desktop/src/main/remote-desktop-viewer/safety.ts
+++ b/apps/desktop/src/main/remote-desktop-viewer/safety.ts
@@ -79,6 +79,7 @@ export class ViewerSafety {
private localReadCache: LocalClipboardReadCache = {};
private applied = new Map<string, boolean>();
private notices = new Map<string, string>();
+ private clipboardNoticeVersion = 0;
private retryAt = 0;
private failures = 0;
private privacyActive = false;
@@ -104,6 +105,7 @@ export class ViewerSafety {
this.notices.get('clipboardSync') ??
null,
clipboardProgress: null,
+ clipboardNoticeVersion: this.clipboardNoticeVersion,
};
}
private recordClipboardFailure(error: unknown): void {
@@ -219,12 +221,13 @@ export class ViewerSafety {
localReadCache: this.localReadCache,
trace: (stage, code) => {
if (!valid()) return;
- if (stage === 'content-skipped')
+ if (stage === 'content-skipped') {
+ this.clipboardNoticeVersion++;
this.notices.set(
'clipboardSync',
code === 'CLIPBOARD_TOO_LONG' ? 'clipboardSyncTooLarge' : 'clipboardSyncSkipped',
);
- else if (
+ } else if (
[
'phone-to-computer-complete',
'computer-to-phone-complete',
diff --git a/apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx b/apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx
index 7cef178be0..15ff3c94f8 100644
--- a/apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx
+++ b/apps/desktop/src/renderer/features/remote-desktop/RemoteDesktopViewerWindow.tsx
@@ -90,13 +90,14 @@ export function RemoteDesktopViewerWindow() {
const activePanel = useRef(settings);
const safetyNotice = state?.safety.notice;
const clipboardNotice = safetyNotice?.startsWith('clipboardSync') ? safetyNotice : null;
+ const clipboardNoticeVersion = state?.safety.clipboardNoticeVersion;
const persistentSafetyNotice = clipboardNotice ? null : safetyNotice;
useEffect(() => {
if (!clipboardNotice) return;
// Same status from idle polls does not reopen or prolong a notification.
const id = toast.info(t(`remoteDesktop.${clipboardNotice}`), { duration: 4000 });
return () => toast.dismiss(id);
- }, [clipboardNotice, t]);
+ }, [clipboardNotice, clipboardNoticeVersion, t]);
// Only the latest clipboard click reports; queued earlier transfers stay silent.
const clipboardAttempt = useRef(0);
activePanel.current = settings;
diff --git a/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerWindow.test.tsx b/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerWindow.test.tsx
index 7628594140..2b6abed9f0 100644
--- a/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerWindow.test.tsx
+++ b/apps/desktop/src/renderer/features/remote-desktop/__tests__/viewerWindow.test.tsx
@@ -121,6 +121,15 @@ it('shows clipboard status as a transient toast without retry and keeps privacy
act(() => lifecycle.update?.({ ...snapshot }));
expect(getToastSnapshot()).toHaveLength(0);
+ // A new item with the same failure is visible; idle polls still do not extend it.
+ const next = { ...snapshot, safety: { ...snapshot.safety, clipboardNoticeVersion: 2 } };
+ act(() => lifecycle.update?.(next));
+ expect(screen.getByText(text)).toBeDefined();
+ await act(async () => vi.advanceTimersByTimeAsync(3000));
+ act(() => lifecycle.update?.({ ...next }));
+ await act(async () => vi.advanceTimersByTimeAsync(1400));
+ expect(getToastSnapshot()).toHaveLength(0);
+
// A distinct size failure gets its own short notice; recovery removes it early.
act(() =>
lifecycle.update?.({
diff --git a/apps/desktop/src/shared/remoteDesktopViewer.ts b/apps/desktop/src/shared/remoteDesktopViewer.ts
index a0971c6112..845eb6137f 100644
--- a/apps/desktop/src/shared/remoteDesktopViewer.ts
+++ b/apps/desktop/src/shared/remoteDesktopViewer.ts
@@ -55,6 +55,8 @@ export const DEFAULT_VIEWER_PREFERENCES: RemoteViewerPreferences = {
export interface RemoteViewerSafety {
privacyActive: boolean;
notice: string | null;
+ /** New skipped clipboard item; idle polls retain the same occurrence. Local IPC only. */
+ clipboardNoticeVersion?: number;
clipboardProgress: number | null;
}
/**
@@ -74,9 +76,16 @@ export function parseRememberedViewerResolution(value: unknown): RememberedViewe
return null;
const width = v.width as number,
height = v.height as number;
- if (v.kind === 'mode' && typeof v.modeId === 'string' && v.modeId.length > 0 && v.modeId.length <= 256)
+ if (
+ v.kind === 'mode' &&
+ typeof v.modeId === 'string' &&
+ v.modeId.length > 0 &&
+ v.modeId.length <= 256
+ )
return { kind: 'mode', modeId: v.modeId, width, height };
- return v.kind === 'fit' && width <= 2560 && height <= 2560 ? { kind: 'fit', width, height } : null;
+ return v.kind === 'fit' && width <= 2560 && height <= 2560
+ ? { kind: 'fit', width, height }
+ : null;
}
/** A request Main asks the viewer window to send over its live media data channel. */
@@ -87,9 +96,7 @@ export interface RemoteViewerChannelRequest {
}
/** How a channel request ended; `relay` means it was never sent, so Main may use the relay. */
export type RemoteViewerChannelOutcome =
- | { kind: 'result'; value: unknown }
- | { kind: 'relay' }
- | { kind: 'error'; code: string };
+ { kind: 'result'; value: unknown } | { kind: 'relay' } | { kind: 'error'; code: string };
export function parseRemoteViewerChannelOutcome(value: unknown): RemoteViewerChannelOutcome | null {
if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
diff --git a/docs/remote-desktop.md b/docs/remote-desktop.md
index bb4e54948b..9dbcae7577 100644
--- a/docs/remote-desktop.md
+++ b/docs/remote-desktop.md
@@ -88,7 +88,11 @@ Connecting establishes both clipboard baselines silently, including empty,
unsupported or oversized existing items. Empty later snapshots do not erase
the other clipboard or compete with a real copy. Desktop clipboard sync notices
use the shared transient toast (4 seconds, no retry action); unchanged status
-polls do not reopen it. Unsupported formats and size limits have distinct copy.
+polls do not reopen it, while a newly skipped item gets a fresh notice even when
+its failure type matches the previous one. The occurrence counter stays in local
+Main-to-Renderer status; it does not change the device-link protocol. Unsupported
+formats and size limits have distinct copy, including when the other peer was
+cleared in the same pass.
Permission denials pause background attempts, but focus and copy/paste wakeups
check again so returning after granting access resumes without a retry button.
diff --git a/packages/device-link/src/__tests__/clipboardSync.test.ts b/packages/device-link/src/__tests__/clipboardSync.test.ts
index 1a30eaadfb..d707d97cf4 100644
--- a/packages/device-link/src/__tests__/clipboardSync.test.ts
+++ b/packages/device-link/src/__tests__/clipboardSync.test.ts
@@ -122,3 +122,75 @@ it("does not accept an empty remote read whose version changed during the read",
await expect(h.engine.tick()).rejects.toThrow("CLIPBOARD_CHANGED");
expect(h.write).not.toHaveBeenCalled();
});
+
+it.each([
+ ["local", "CLIPBOARD_UNSUPPORTED"],
+ ["remote", "CLIPBOARD_UNSUPPORTED"],
+ ["local", "CLIPBOARD_TOO_LONG"],
+ ["remote", "CLIPBOARD_TOO_LONG"],
+] as const)(
+ "an empty %s peer does not conflict with %s on the other peer",
+ async (side, code) => {
+ const h = fixture();
+ await h.engine.tick();
+ const other = side === "local" ? "remote" : "local";
+ h[side].version++;
+ h[side].error = "CLIPBOARD_EMPTY";
+ h[other].version++;
+ h[other].error = code;
+ await h.engine.tick();
+ expect(h.trace).toHaveBeenLastCalledWith("content-skipped", code);
+ expect(h.write).not.toHaveBeenCalled();
+ expect(
+ h.request.mock.calls.some(
+ ([m]) => m.op === "clipboardContent" && m.action === "paste",
+ ),
+ ).toBe(false);
+ h.trace.mockClear();
+ const reads = h.read.mock.calls.length;
+ const requests = h.request.mock.calls.length;
+ await h.engine.tick();
+ expect(h.trace).not.toHaveBeenCalled();
+ expect(h.read).toHaveBeenCalledTimes(reads);
+ expect(h.request).toHaveBeenCalledTimes(requests + 1); // Version only.
+ // The next supported copy still reaches the cleared peer.
+ h[other].version++;
+ h[other].error = "";
+ h[other].json = '{"text":"next copy"}';
+ await h.engine.tick();
+ expect(h.local.json).toBe(h.remote.json);
+ expect(h[side].error).toBe("");
+ },
+);
+
+it.each(["local", "remote"] as const)(
+ "does not consume skipped snapshots when the empty %s peer changes during its read",
+ async (side) => {
+ const h = fixture();
+ await h.engine.tick();
+ const other = side === "local" ? "remote" : "local";
+ h[side].version++;
+ h[side].error = "CLIPBOARD_EMPTY";
+ h[other].version++;
+ h[other].error = "CLIPBOARD_TOO_LONG";
+ if (side === "local") {
+ const read = h.read.getMockImplementation()!;
+ h.read.mockImplementationOnce(async () => {
+ h.local.version++;
+ return read();
+ });
+ } else {
+ const request = h.request.getMockImplementation()!;
+ h.request.mockImplementation(async (message) => {
+ if (message.op === "clipboardContent" && message.action === "copy")
+ h.remote.version++;
+ return request(message);
+ });
+ }
+ await expect(h.engine.tick()).rejects.toThrow("CLIPBOARD_CHANGED");
+ expect(
+ h.trace.mock.calls.some(([stage]) => stage === "content-skipped"),
+ ).toBe(false);
+ expect(h.write).not.toHaveBeenCalled();
+ },
+);
diff --git a/packages/device-link/src/clipboardSync.ts b/packages/device-link/src/clipboardSync.ts
index 1e219f5b0d..25c223b8d9 100644
--- a/packages/device-link/src/clipboardSync.ts
+++ b/packages/device-link/src/clipboardSync.ts
@@ -226,6 +226,7 @@ export class ClipboardSync {
let remote: string | undefined;
let readLocalDigest: string | undefined;
let readRemoteDigest: string | undefined;
+ const readErrors: { local?: string; remote?: string } = {};
try {
this.check();
local = await this.deps.localVersion();
@@ -274,13 +275,18 @@ export class ClipboardSync {
let localChanged = local !== this.baseline.local;
let remoteChanged = remote !== this.baseline.remote;
if (!localChanged && !remoteChanged) return;
- const empty = (error: unknown): undefined => {
- if (clipboardSyncErrorCode(error) !== "CLIPBOARD_EMPTY") throw error;
- return undefined;
- };
+ // Read both changed peers even when one is unsupported. Only a proven
+ // non-empty change can compete with the other side's copy.
+ const skipped =
+ (side: "local" | "remote") =>
+ (error: unknown): undefined => {
+ if (!skippableContent(error)) throw error;
+ readErrors[side] = clipboardSyncErrorCode(error);
+ return undefined;
+ };
const localContent = localChanged
? await this.readLocal(local)
- .catch(empty)
+ .catch(skipped("local"))
.then((content) => {
if (!content) return undefined;
readLocalDigest = content.digest;
@@ -289,7 +295,7 @@ export class ClipboardSync {
: undefined;
const remoteContent = remoteChanged
? await this.readRemote(remote)
- .catch(empty)
+ .catch(skipped("remote"))
.then((content) => {
if (!content) return undefined;
readRemoteDigest = content.digest;
@@ -306,6 +312,10 @@ export class ClipboardSync {
)
throw new Error("CLIPBOARD_CHANGED");
this.check();
+ const skippedCode = [readErrors.local, readErrors.remote].find(
+ (code) => code && code !== "CLIPBOARD_EMPTY",
+ );
+ if (skippedCode) throw new Error(skippedCode);
// Empty is not a copy to propagate or a competing edit. A real copy on
// the other side may still sync; never erase it with an empty clipboard.
const localDigest = localChanged
@@ -379,7 +389,10 @@ export class ClipboardSync {
) {
this.check();
const bothChanged =
- local !== this.baseline.local && remote !== this.baseline.remote;
+ local !== this.baseline.local &&
+ remote !== this.baseline.remote &&
+ readErrors.local !== "CLIPBOARD_EMPTY" &&
+ readErrors.remote !== "CLIPBOARD_EMPTY";
Object.assign(this.baseline, {
local,
remote,