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) {