clipboard: de-duplicate in-band chunks by Id

A machine pair keeps two control connections and PreProcess exempts
ClipboardText/ClipboardImage from the replay ring (Receiver.cs:40-82), so
every chunk is delivered twice. Windows reassembles from the one socket the
first chunk arrived on (Clipboard.cs:308-373) and never sees it; ours is
keyed by sending machine, so the copies interleaved and corrupted the
payload. Reassembler now keeps a 50-entry ring of chunk Ids, which works
because contracts C1 stamps one Id per chunk for both deliveries.
docs/protocol.md §10.7 note 11.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 04:56:31 -07:00
1 parent 105a34cb45
commit dd26befe58
3 files changed
+59 -5

No files matched your search

+16 -4
View File
@@ -33,7 +33,11 @@ Confirmed or corrected while porting; every correction is written up in
`ClipboardDataEnd (76)`, last chunk zero-padded with no length field
(`Clipboard.cs:253-306`). They are app-stamped and forwarded (C1).
The receiver keeps one `Reassembler` per sender; trailing zeros are
part of the DEFLATE stream and the decoder tolerates them. The
part of the DEFLATE stream and the decoder tolerates them. **Each chunk
arrives twice** — a machine pair has two connections and the replay
ring exempts these two types — so `Reassembler` keeps its own ring of
chunk `Id`s; keying reassembly by sender rather than by socket is what
makes the duplicate visible (§10.7 note 11). The
"abort after 100 interleaved foreign packets" rule is **not** ported —
it is a property of the Windows blocking read loop, which we do not
have; a size cap and a 30 s idle timeout take its place (§10.7 note 9).
@@ -155,7 +159,7 @@ public enum ClipboardCodec {
public static func string(fromUTF16LE:) throws -> String
}
public final class Reassembler { // one per sending machine
public static let sizeLimit: Int
public static let sizeLimit, idWindow: Int
public init(now: TimeInterval)
public func feed(_ packet: Packet, now: TimeInterval) -> Data? // payload on DataEnd
public private(set) var abandoned, isImage: Bool
@@ -409,6 +413,13 @@ was not needed.
- The "100 interleaved foreign packets" abort became a size cap plus an
idle timeout: we have no blocking read loop for foreign packets to
interleave with (§10.7 note 9).
- `Reassembler` de-duplicates by chunk `Id`. Found while re-reading the
first pass: a peer pair has two control connections, `SkSend` writes
the same chunk to both, and `PreProcess` exempts `ClipboardText` and
`ClipboardImage` from the replay ring — so a reassembly keyed by
sending machine, as ours is, interleaves the two copies and corrupts
the payload where Windows's socket-keyed loop merely does the work
twice (§10.7 note 11).
- The download folder is created on the first received file, not at
startup, so an app that never receives one leaves the Desktop alone.
@@ -431,8 +442,9 @@ clipboard, no listener outside a few seconds on 127.0.0.1:51599):
prefix; the 1024-byte header round trip including a Windows path, the
`0*<message>` error form and `..\..` traversal reduced to its last
component; padding block-aligned for 0, 1, 15, 16, 31, 32, 119 and
1 048 577; the `Reassembler` size cap; an untagged blob read as plain
text. All pass.
1 048 577; the `Reassembler` size cap; the same chunk stream delivered
twice, interleaved, collapsing to one payload; an untagged blob read as
plain text. 170 checks, all pass.
- a two-channel loopback harness on 127.0.0.1:51599, up for six seconds:
a text payload pulled and decoded correctly; a 5 000-byte file pushed
and staged with the right size and name; a 3 MB image pulled across