session-matrix: PacketSequencer is thread-safe (app-engine request 1)

Cross-lane edit, authorised by the lead. Contracts C1 gives the process
one sequencer and two callers: the Engine actor stamps application
packets, each `Session` stamps its own handshake, heartbeat, `Hi` and
`ByeBye` on the channel's queue (`Session.swift:402-403`). `value &+ 1`
with no synchronisation can lose an update and hand two packets the same
`Id`, which the peer's replay ring drops (`Receiver.cs:40-82`). The
counter now lives under an `NSLock` — Foundation only, so MWBCore's
layering holds — and the class is `@unchecked Sendable`, which is the
guarantee Windows gets from `Interlocked.Increment` (`Common.cs:916`).
`ReplayFilter` is unchanged: one queue touches it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 03:56:52 -07:00
1 parent 02df633a01
commit 8125cb1ede
2 files changed
+30 -8

No files matched your search

+10 -3
View File
@@ -128,7 +128,7 @@ does I/O, reads a clock or logs.
```swift
// Sequencing.swift — the two process-wide counters (§1.4)
public final class PacketSequencer { // not Sendable: app-wide, one queue
public final class PacketSequencer: @unchecked Sendable { // thread-safe: NSLock
public init(start: Int32 = 0) // resumes the PackageID setting
public func next() -> Int32 // pre-increment, wrapping
public var current: Int32 { get } // persist this back
@@ -316,8 +316,15 @@ Rules the consuming lanes must honour:
and after the replay ring (CLAUDE.md invariant 2). `.close` is always the
last event a session emits; close the socket and drop the session.
- **One `PacketSequencer` and one `ReplayFilter` per process**, shared by
every `Session`, as on Windows. Neither is `Sendable`; nor are `Session`
and `Switcher`. Everything in this lane lives on one queue.
every `Session`, as on Windows. `PacketSequencer` is **thread-safe and
`Sendable`** — it holds its counter under an `NSLock` — because contracts
C1 gives it two callers by design: the Engine actor stamps application
packets while a `Session` stamps its own handshake, heartbeat, `Hi` and
`ByeBye` on the channel's queue, and a lost update there would hand two
packets the same `Id` (amended 2026-09-16 at the app-engine lane's
request, with the lead's authorisation). `ReplayFilter` is **not**
`Sendable`; nor are `Session` and `Switcher` — those are touched on one
queue only.
- **`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(_:)`