session-matrix: Session cannot stamp an application packet (contracts C1)

Session.send(_:) is gone as a public entry point. Application traffic gets
its Id once from the process-wide PacketSequencer and reaches each target
connection through forward(_:), which never re-stamps; the per-connection
stamp survives only as `private func originate(_:)` for the packets
Windows also sends per connection — the handshake pair, heartbeats, Hi and
ByeBye. Nothing outside the type can now sequence an application packet.

README Assumptions and Handshakes say this is API-enforced rather than a
convention. protocol.md gains §10.2 note 11: SkSend increments PackageID
once before the socket loop (Common.cs:914) and hands the same DATA to
every matching socket, which is exactly what makes the receiver's 50-entry
replay ring collapse a peer pair's duplicate delivery.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 01:00:39 -07:00
1 parent 9252158bbc
commit 9980c685e2
3 files changed
+62 -27

No files matched your search

+27 -17
View File
@@ -219,23 +219,21 @@ public final class Session {
guard status == .connected, !finished, now >= nextHeartbeat else { return }
nextHeartbeat = now + heartbeatInterval
let type: PackageType = config.blockScreenSaver && userActive ? .awake : .heartbeat
send(broadcast(type))
originate(broadcast(type))
}
/// Queues a packet, stamping `Id` and `Src` as `SkSend` does
/// (`Common.cs:916`, `SocketStuff.cs:666-669`). Use this for anything the
/// app originates.
public func send(_ packet: Packet) {
var p = packet
p.id = sequencer.next()
sendDirect(p)
}
/// Queues a packet that already carries its `Id`, for a broadcast the app
/// sequenced once and is sending to several connections — which is what
/// `SkSend` does with one `Id` and many sockets (`Common.cs:912-930`), and
/// what keeps the receiver's replay ring collapsing a peer pair's two
/// copies into one. `Src` is still filled in if it is `NONE`.
/// Queues an application packet the app has **already** sequenced, for
/// delivery on this one connection.
///
/// This is the only way in for application traffic (contracts C1): the app
/// stamps one `Id` with the process-wide `PacketSequencer` and hands the
/// identical `Packet` to every target session, which is what `SkSend` does
/// with one `Id` and many sockets (`Common.cs:912-930`) and what keeps the
/// receiver's 50-entry replay ring collapsing a peer pair's two copies into
/// one (`Receiver.cs:40-82`). There is deliberately no entry point that
/// stamps an application packet per connection. `Src` is still filled in
/// here when it is `NONE`, exactly as `TcpSend` does
/// (`SocketStuff.cs:666-669`).
public func forward(_ packet: Packet) {
sendDirect(packet)
}
@@ -253,7 +251,7 @@ public final class Session {
/// Queues `ByeBye (4)` on the way out (`Common.cs:612-616`).
public func sayGoodbye() {
send(broadcast(.byeBye))
originate(broadcast(.byeBye))
}
/// Framed bytes to write through `CipherWriter`, in order. Drains the queue.
@@ -361,7 +359,7 @@ public final class Session {
peerID = packet.src
report(.connected)
pending.append(.connected(peerName: name, peerID: packet.src))
send(broadcast(.heartbeat)) // SendHeartBeat(initial: true), SocketStuff.cs:1451
originate(broadcast(.heartbeat)) // SendHeartBeat(initial: true), SocketStuff.cs:1451
}
/// `Receiver.PreProcess` (`Receiver.cs:40-82`): drop what framing rejected,
@@ -375,6 +373,18 @@ public final class Session {
// MARK: - Sending
/// Stamps `Id` on a packet this session **originates** and queues it.
///
/// Private on purpose (contracts C1): the only packets a connection may
/// sequence for itself are the ones that are per-connection on Windows too
/// — the handshake pair, heartbeats, `Hi` and `ByeBye`. Everything the app
/// originates is sequenced once and comes in through `forward(_:)`.
private func originate(_ packet: Packet) {
var p = packet
p.id = sequencer.next() // Common.cs:916
sendDirect(p)
}
/// Writes a packet without touching `Id`, which is what `TcpSend` does:
/// only `SkSend` sequences (`SocketStuff.cs:663-678`).
private func sendDirect(_ packet: Packet) {
+19 -10
View File
@@ -53,10 +53,15 @@ the Windows source again.
`PackageID` setting and the app stores it back; this lane exposes
`current` for that.
- **Sequence once, distribute unchanged (contracts C1).** Application
packets are stamped by the app and handed to each target session via
`forward(_:)`; `Session` stamps only the packets it originates
(handshake, heartbeats, `Hi`, `ByeBye`). There is no public API to
send an unstamped application packet on one connection.
packets are stamped by the app — one `Id` from the process-wide
`PacketSequencer` — and the identical `Packet` is handed to each target
session via `forward(_:)`, which never re-stamps. `Session` stamps ids
itself only for the packets it originates (`Handshake`, `HandshakeAck`,
heartbeats, `Hi`, `ByeBye`), which are per-connection on Windows too;
that path is `private func originate(_:)`. This is enforced by the API,
not by convention: as of 2026-09-16 there is **no** public
`Session.send`-style entry point, so a caller cannot send an unstamped
application packet on one connection even by mistake.
- **Each `Session` beats for its own connection.** Windows broadcasts one
heartbeat from a global timer to every socket, so a peer pair's two
connections carry the *same* `Id` and the receiver's replay ring
@@ -164,8 +169,7 @@ public final class Session { // not Sendable: one per connecti
public func noteReceiveError(bytesReceived: Int) // a 0-byte / short read
public func report(_ new: SocketStatus) // .resolving, .sendError, …
public func tick(now: TimeInterval, userActive: Bool)
public func send(_ packet: Packet) // stamps Id + Src, frames, queues
public func forward(_ packet: Packet) // already sequenced: Src only
public func forward(_ packet: Packet) // app-stamped: Src + framing only
public func probe() // Hi
public func sayGoodbye() // ByeBye
public func drain() -> [Data] // write these through CipherWriter
@@ -314,10 +318,15 @@ Rules the consuming lanes must honour:
and `Switcher`. Everything in this lane lives on one queue.
- **`Switcher` never does I/O.** It returns `[SwitchAction]`; the app
performs them in order. A `.send` packet is already sequenced and
addressed, so it goes to every connection through `Session.forward(_:)`,
**not** `Session.send(_:)` — one broadcast must keep one `Id` across a
peer's two connections or the receiver's replay ring will not collapse
the duplicate.
addressed, so it goes to every connection through `Session.forward(_:)`
— the only application-traffic entry point there is. One broadcast must
keep one `Id` across a peer's two connections or the receiver's replay
ring will not collapse the duplicate (contracts C1).
- **Anything else the app originates is stamped once, by the app.** Input,
`HideMouse`, `MachineSwitched`, `NextMachine`, `Matrix` and clipboard
chunks take an `Id` from the single `PacketSequencer` and are then
`forward`ed to each target session unchanged. `Session` sequences only
its own handshake, heartbeats, `Hi` and `ByeBye`.
- **`.parkCursor` records a position; it moves the OS cursor only when
`destinationIsRemote || byHideMouseMessage`.** `Switcher` pre-evaluates
both halves of the `HideMouseCursor` gate (`Common.cs:1488`) so no
+16
View File
@@ -592,3 +592,19 @@ sees them. One subsection per lane, appended, never rewritten.
- The trusted branch is guarded by `receivedCount > 0` (`:1531`), so a
0-byte read never reaches `Receiver.ProcessPackage`; a short read does,
and `PreProcess` drops it for being `Invalid` (`Receiver.cs:42-50`).
11. **`SkSend` stamps one `Id` and writes the same `DATA` to every matching
socket.** The increment happens once, before the loop
(`Common.cs:914`), and the same struct is then handed to `TcpSend` for
each socket whose `MachineId` matches `Des` — or, for `Des == ID.ALL`,
every socket that is not `exceptDes` and whose machine is in the matrix
(`:919-929`). `TcpSend` fills `Src` when it is `ID.NONE` and touches
nothing else (`SocketStuff.cs:666-669`). So a peer reached over two
connections receives the *same* `Id` twice and the receiver's 50-entry
replay ring collapses the second delivery (`Receiver.cs:40-82`) — which
is the mechanism, not an accident, and the reason contracts C1 requires
the app to stamp an application packet once and distribute it unchanged.
A per-connection stamp would give the duplicate a fresh `Id` and defeat
the ring. Only the packets Windows itself sends per connection — the
handshake pair, the heartbeat written on a fresh connection
(`SocketStuff.cs:1451`), `Hi` and `ByeBye` — go out through `TcpSend`
with no `SkSend` in front of them.