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): 在共享同步器统一冲突后的复制恢复

Dasha9ebc09e83c2
diff --git a/apps/desktop/src/main/remote-desktop-viewer/safety.ts b/apps/desktop/src/main/remote-desktop-viewer/safety.ts
index 43cca68cc1..044f6e2c44 100644
--- a/apps/desktop/src/main/remote-desktop-viewer/safety.ts
+++ b/apps/desktop/src/main/remote-desktop-viewer/safety.ts
@@ -110,13 +110,6 @@ export class ViewerSafety {
}
private recordClipboardFailure(error: unknown): void {
const failure = viewerClipboardFailure(error, this.failures);
- if (failure.code === 'CLIPBOARD_CONFLICT') {
- // Preserve counters so an idle poll cannot choose a winner. Forget the
- // conflicting digests so the next copy selects that side even when its
- // content is unchanged; a successful transfer restores normal deduping.
- delete this.baseline.localDigest;
- delete this.baseline.remoteDigest;
- }
if (!failure.transient) this.failures++;
// Only the computer refusing sync needs it re-enabled; a local helper failure does not.
if (failure.code === 'DESKTOP_CLIPBOARD_UNAVAILABLE') this.applied.delete('clipboardSync');
diff --git a/apps/mobile/src/remote-desktop/clipboardSync.test.ts b/apps/mobile/src/remote-desktop/clipboardSync.test.ts
index cdc4f24561..976c016063 100644
--- a/apps/mobile/src/remote-desktop/clipboardSync.test.ts
+++ b/apps/mobile/src/remote-desktop/clipboardSync.test.ts
@@ -223,7 +223,7 @@ describe("automatic clipboard synchronization", () => {
await expect(h.engine.tick()).rejects.toThrow("CLIPBOARD_CONFLICT");
expect(h.write).not.toHaveBeenCalled();
await h.engine.tick();
- h[direction]("4");
+ h[direction]("4", JSON.stringify({ text: `${direction}-2` }));
await h.engine.tick();
await h.engine.tick();
expect(h.write).toHaveBeenCalledTimes(direction === "remote" ? 1 : 0);
@@ -413,7 +413,7 @@ it.each(["local", "remote"] as const)(
).toBe(1);
},
);
-it("retains a successfully read new local digest when the remote read is unsupported", async () => {
+it("clears both conflict digests so recopying identical local content can recover", async () => {
const h = harness();
const baseline: ClipboardSyncBaseline = { local: "", remote: "" };
const engine = new ClipboardSync(h.deps, baseline);
@@ -429,11 +429,17 @@ it("retains a successfully read new local digest when the remote read is unsuppo
// Both changed: both copies are kept, and the user is told rather than the
// readable copy silently never arriving.
await expect(engine.tick()).rejects.toThrow("CLIPBOARD_CONFLICT");
- expect(baseline.localDigest).toBe(
- clipboardDigest(JSON.stringify({ text: "local-2" })),
- );
+ expect(baseline.localDigest).toBeUndefined();
expect(baseline.remoteDigest).toBeUndefined();
expect(h.write).not.toHaveBeenCalled();
+ h.request.mockImplementation(original);
+ h.local("4", JSON.stringify({ text: "local-2" }));
+ await engine.tick();
+ expect(
+ h.request.mock.calls.filter(
+ ([m]) => m.op === "clipboardContent" && m.action === "commit",
+ ),
+ ).toHaveLength(1);
});
it("an unreadable local item next to a new computer copy is a conflict; the next copy on either side syncs", async () => {
@@ -461,7 +467,7 @@ it("an unreadable local item next to a new computer copy is a conflict; the next
expect(h.write).not.toHaveBeenCalled();
await expect(engine.tick()).resolves.toBeUndefined();
// Copying again on the computer brings that copy over.
- h.remote("4");
+ h.remote("4", JSON.stringify({ text: "remote-2" }));
await engine.tick();
expect(h.write).toHaveBeenCalledTimes(1);
});
diff --git a/docs/remote-desktop.md b/docs/remote-desktop.md
index dd05216774..71ef6820fe 100644
--- a/docs/remote-desktop.md
+++ b/docs/remote-desktop.md
@@ -78,8 +78,9 @@ notice); unexplained failures back off to at most 5 s and reset on focus or a
shortcut. A multi-item local clipboard (several files) is skipped rather than
failing the pass; if the computer copied something in the same interval, that is
reported as a conflict like any simultaneous change, and copying again on either
-side syncs, even when that copy repeats identical readable content. The desktop
-adapter retains conflict counters but clears their content digests: idle polls
+side syncs, even when that copy repeats identical readable content. The shared
+synchronizer retains conflict counters but clears both content digests for Desktop
+and Mobile, including when either peer is unreadable: idle polls
preserve both clipboards, and the next copy chooses the source. A successful
transfer restores ordinary content deduplication. Contents stay in Main/native code and do not cross the viewer
Renderer bridge.
diff --git a/packages/device-link/src/__tests__/clipboardSync.test.ts b/packages/device-link/src/__tests__/clipboardSync.test.ts
index b5bf625694..d708ebe252 100644
--- a/packages/device-link/src/__tests__/clipboardSync.test.ts
+++ b/packages/device-link/src/__tests__/clipboardSync.test.ts
@@ -46,6 +46,62 @@ function fixture() {
return { engine, local, remote, trace, write, read, request };
}
+it.each([
+ ["", "local"],
+ ["", "remote"],
+ ["CLIPBOARD_UNSUPPORTED", "local"],
+ ["CLIPBOARD_UNSUPPORTED", "remote"],
+ ["CLIPBOARD_TOO_LONG", "local"],
+ ["CLIPBOARD_TOO_LONG", "remote"],
+] as const)(
+ "after a conflict with peer error %s, an identical %s recopy selects the source",
+ async (code, chosen) => {
+ const h = fixture();
+ await h.engine.tick();
+ const other = chosen === "local" ? "remote" : "local";
+ h.local.version++;
+ h.remote.version++;
+ h.local.json = '{"text":"local choice"}';
+ h.remote.json = '{"text":"remote choice"}';
+ h[other].error = code;
+ await expect(h.engine.tick()).rejects.toThrow("CLIPBOARD_CONFLICT");
+ h.trace.mockClear();
+ await h.engine.tick();
+ expect(h.trace).not.toHaveBeenCalled();
+ expect(h.write).not.toHaveBeenCalled();
+ expect(
+ h.request.mock.calls.some(
+ ([m]) => m.op === "clipboardContent" && m.action === "paste",
+ ),
+ ).toBe(false);
+ const selected = h[chosen].json;
+ h[chosen].version++;
+ await h.engine.tick();
+ expect(h.local.json).toBe(selected);
+ expect(h.remote.json).toBe(selected);
+ expect(h[other].error).toBe("");
+ expect(h.trace).toHaveBeenLastCalledWith(
+ chosen === "local"
+ ? "phone-to-computer-complete"
+ : "computer-to-phone-complete",
+ );
+ const writes = h.write.mock.calls.length;
+ const pastes = h.request.mock.calls.filter(
+ ([m]) => m.op === "clipboardContent" && m.action === "paste",
+ ).length;
+ // A completed transfer restores deduplication, including its delayed echoes.
+ h.local.version++;
+ h.remote.version++;
+ await h.engine.tick();
+ expect(h.write).toHaveBeenCalledTimes(writes);
+ expect(
+ h.request.mock.calls.filter(
+ ([m]) => m.op === "clipboardContent" && m.action === "paste",
+ ),
+ ).toHaveLength(pastes);
+ },
+);
+
it.each(["CLIPBOARD_EMPTY", "CLIPBOARD_UNSUPPORTED", "CLIPBOARD_TOO_LONG"])(
"silently establishes both initial %s baselines, then syncs a new copy",
async (code) => {
diff --git a/packages/device-link/src/clipboardSync.ts b/packages/device-link/src/clipboardSync.ts
index 878cceb746..a652d51374 100644
--- a/packages/device-link/src/clipboardSync.ts
+++ b/packages/device-link/src/clipboardSync.ts
@@ -51,6 +51,17 @@ export class ClipboardSync {
private check() {
if (!this.deps.current()) throw new Error("DESKTOP_LEASE_EXPIRED");
}
+ private conflict(local: string, remote: string): never {
+ // Preserve counters so idle polls cannot pick a winner. Both adapters must
+ // allow the next explicit copy, even when it repeats the conflicting content.
+ Object.assign(this.baseline, {
+ local,
+ remote,
+ localDigest: undefined,
+ remoteDigest: undefined,
+ });
+ throw new Error("CLIPBOARD_CONFLICT");
+ }
private async remoteVersion() {
const { version } = await this.deps.request<{ version: string }>({
op: "clipboardVersion",
@@ -347,15 +358,7 @@ export class ClipboardSync {
localChanged = false;
remoteChanged = false;
}
- if (localChanged && remoteChanged) {
- Object.assign(this.baseline, {
- local,
- remote,
- localDigest,
- remoteDigest,
- });
- throw new Error("CLIPBOARD_CONFLICT");
- }
+ if (localChanged && remoteChanged) this.conflict(local, remote);
if (localChanged && localContent) {
const version = await this.writeRemote(
localContent.json,
@@ -430,7 +433,7 @@ export class ClipboardSync {
// simultaneous change, both copies are kept, but the user is told so
// instead of the readable copy silently never arriving.
if (bothChanged && code !== "CLIPBOARD_EMPTY")
- throw new Error("CLIPBOARD_CONFLICT");
+ this.conflict(local, remote);
return;
}
throw error;