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): 清空后允许重新同步相同内容

Dash7f184aaaa98b
diff --git a/docs/remote-desktop.md b/docs/remote-desktop.md
index 9dbcae7577..4bf0d9ea70 100644
--- a/docs/remote-desktop.md
+++ b/docs/remote-desktop.md
@@ -86,7 +86,11 @@ Renderer bridge.
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
+the other clipboard or compete with a real copy. A version-checked clear resets
+both content digests while retaining counters: idle polls leave the other side
+alone, but copying the same content again can restore the cleared side. Successful
+sync restores normal deduplication; an initially empty clipboard only establishes
+the baseline and does not import old content. Desktop clipboard sync notices
use the shared transient toast (4 seconds, no retry action); unchanged status
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
diff --git a/packages/device-link/src/__tests__/clipboardSync.test.ts b/packages/device-link/src/__tests__/clipboardSync.test.ts
index d707d97cf4..b5bf625694 100644
--- a/packages/device-link/src/__tests__/clipboardSync.test.ts
+++ b/packages/device-link/src/__tests__/clipboardSync.test.ts
@@ -107,6 +107,61 @@ it.each(["CLIPBOARD_UNSUPPORTED", "CLIPBOARD_TOO_LONG"])(
},
);
+it.each([
+ ["local", false],
+ ["remote", false],
+ ["local", true],
+ ["remote", true],
+] as const)(
+ "restores identical content to cleared %s (same pass: %s), then resumes deduplication",
+ async (destination, samePass) => {
+ const h = fixture();
+ const source = destination === "local" ? "remote" : "local";
+ const transfers = () =>
+ h.write.mock.calls.length +
+ h.request.mock.calls.filter(
+ ([m]) => m.op === "clipboardContent" && m.action === "paste",
+ ).length;
+ await h.engine.tick();
+ h[destination].version++;
+ h[destination].error = "CLIPBOARD_EMPTY";
+ if (!samePass) {
+ await h.engine.tick();
+ await h.engine.tick();
+ expect(transfers()).toBe(0);
+ }
+ h[source].version++; // Explicitly recopy exactly its previous content.
+ await h.engine.tick();
+ expect(h[destination].error).toBe("");
+ expect(h[destination].json).toBe(h[source].json);
+ expect(transfers()).toBe(1);
+ await h.engine.tick();
+ h[source].version++;
+ h[destination].version++; // Identical rewrites after restoration are deduped.
+ await h.engine.tick();
+ expect(transfers()).toBe(1);
+ },
+);
+
+it.each(["local", "remote"] as const)(
+ "does not import old identical content into an initially empty %s clipboard",
+ async (destination) => {
+ const h = fixture();
+ const source = destination === "local" ? "remote" : "local";
+ h[destination].error = "CLIPBOARD_EMPTY";
+ await h.engine.tick();
+ h[source].version++;
+ await h.engine.tick();
+ expect(h[destination].error).toBe("CLIPBOARD_EMPTY");
+ expect(h.write).not.toHaveBeenCalled();
+ expect(
+ h.request.mock.calls.some(
+ ([m]) => m.op === "clipboardContent" && m.action === "paste",
+ ),
+ ).toBe(false);
+ },
+);
+
it("does not accept an empty remote read whose version changed during the read", async () => {
const h = fixture();
await h.engine.tick();
diff --git a/packages/device-link/src/clipboardSync.ts b/packages/device-link/src/clipboardSync.ts
index 25c223b8d9..878cceb746 100644
--- a/packages/device-link/src/clipboardSync.ts
+++ b/packages/device-link/src/clipboardSync.ts
@@ -227,6 +227,7 @@ export class ClipboardSync {
let readLocalDigest: string | undefined;
let readRemoteDigest: string | undefined;
const readErrors: { local?: string; remote?: string } = {};
+ let cleared = false;
try {
this.check();
local = await this.deps.localVersion();
@@ -312,6 +313,10 @@ export class ClipboardSync {
)
throw new Error("CLIPBOARD_CHANGED");
this.check();
+ // A validated clear breaks content deduplication for both peers: the
+ // next version change may explicitly copy the same content back. Keep
+ // counters so clearing alone (or an idle poll) never initiates a write.
+ cleared = Object.values(readErrors).includes("CLIPBOARD_EMPTY");
const skippedCode = [readErrors.local, readErrors.remote].find(
(code) => code && code !== "CLIPBOARD_EMPTY",
);
@@ -326,9 +331,17 @@ export class ClipboardSync {
: this.baseline.remoteDigest;
if (localChanged && !localContent) localChanged = false;
if (remoteChanged && !remoteContent) remoteChanged = false;
- if (localContent && localContent.digest === this.baseline.localDigest)
+ if (
+ !cleared &&
+ localContent &&
+ localContent.digest === this.baseline.localDigest
+ )
localChanged = false;
- if (remoteContent && remoteContent.digest === this.baseline.remoteDigest)
+ if (
+ !cleared &&
+ remoteContent &&
+ remoteContent.digest === this.baseline.remoteDigest
+ )
remoteChanged = false;
if (localDigest && localDigest === remoteDigest) {
localChanged = false;
@@ -374,8 +387,8 @@ export class ClipboardSync {
Object.assign(this.baseline, {
local,
remote,
- localDigest,
- remoteDigest,
+ localDigest: cleared ? undefined : localDigest,
+ remoteDigest: cleared ? undefined : remoteDigest,
});
this.deps.trace?.("unchanged-content");
}
@@ -391,19 +404,20 @@ export class ClipboardSync {
const bothChanged =
local !== this.baseline.local &&
remote !== this.baseline.remote &&
- readErrors.local !== "CLIPBOARD_EMPTY" &&
- readErrors.remote !== "CLIPBOARD_EMPTY";
+ !cleared;
Object.assign(this.baseline, {
local,
remote,
- // Keep evidence for an unchanged or successfully read peer. Only
- // an unreadable new version loses its digest, never both peers.
- localDigest:
- local === this.baseline.local
+ // Unsupported content alone preserves the other peer's evidence;
+ // a validated clear invalidates deduplication for both peers.
+ localDigest: cleared
+ ? undefined
+ : local === this.baseline.local
? this.baseline.localDigest
: readLocalDigest,
- remoteDigest:
- remote === this.baseline.remote
+ remoteDigest: cleared
+ ? undefined
+ : remote === this.baseline.remote
? this.baseline.remoteDigest
: readRemoteDigest,
});