control-channel: the Awake test moves into Session, where the beat is

Round-2 QA finding: the channel reset beatInputEventCount on its own
clock, out of phase with every connection's heartbeat instant, so a
session could send Heartbeat (20) where Windows sends Awake (21) — and
Awake is what drives the peer's Common.HumanBeingDetected()
(Receiver.cs:245-248), the default-on BlockScreenSaver mechanism.

Windows's beat is the send: SendAwakeBeat() compares
lastRealInputEventCount against Event.RealInputEventCount and re-records
it in the same call (Common.cs:542-559). So the comparison belongs at the
send. Session.tick(now:userActive:) becomes
Session.tick(now:inputEventCount:), taking the raw process-wide counter
(Event.cs:51-55, InputHook.cs:223, 345) and keeping its own
beatInputEventCount — Common.lastRealInputEventCount, Common.cs:543 —
recorded on both branches (:558).

ControlChannel loses lastBeatAt and beatInputEventCount entirely;
noteUserActivity() only increments the counter and tick() hands the same
value to every connection. No userActive-shaped name is left in Sources.

Sources/MWBCore/Session.swift is session-matrix's file, edited for this
fix with the lead's authorisation, as is that lane's Handshakes section.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 01:57:03 -07:00
1 parent 23eddb42b9
commit c03b8cdbcf
5 files changed
+158 -40

No files matched your search

+25 -6
View File
@@ -6,7 +6,7 @@
// Ported from App/Class/SocketStuff.cs MainTCPRoutine (1261-1565) and
// TcpReceiveData (557-610), App/Core/Receiver.cs PreProcess (40-82),
// App/Core/Common.cs SendPackage/SendHeartBeat (499-513), SendAwakeBeat
// (545-558), SendByeBye (612-616) and TestSend (822-847),
// (542-559), SendByeBye (612-616) and TestSend (822-847),
// App/Form/frmScreen.cs the 100 ms timer (285-288, 555-575).
import Foundation
@@ -124,6 +124,14 @@ public final class Session {
private var nextHeartbeat: TimeInterval = .greatestFiniteMagnitude
private var finished = false
/// The real-input counter as it stood when *this* session last beat —
/// `Common.lastRealInputEventCount` (`Common.cs:543`), re-recorded on the way
/// out of every beat (`:558`). It starts at zero because the Windows field
/// does, so the first beat after the process has seen any input at all is an
/// `Awake`; a spurious `Awake` only wakes a peer's screen, while a missed one
/// lets it lock under the user's hands.
private var beatInputEventCount: UInt64 = 0
/// The connection's status.
public private(set) var status: SocketStatus = .na
/// The peer's name, known once the handshake completes.
@@ -212,13 +220,24 @@ public final class Session {
pending.append(.status(new))
}
/// Heartbeats. `userActive` is whether real input happened since the last
/// tick, which is what turns a `Heartbeat` into an `Awake`
/// (`Common.cs:545-552`).
public func tick(now: TimeInterval, userActive: Bool) {
/// Heartbeats. `inputEventCount` is the raw process-wide real-input counter,
/// which is `Event.RealInputEventCount` on Windows (`Event.cs:51-55`, bumped
/// by the hooks at `InputHook.cs:223, 345`); the owner passes the same value
/// to every session on a tick and never interprets it.
///
/// The beat is `SendAwakeBeat` (`Common.cs:545-559`): `Awake` when
/// `BlockScreenSaver` is on *and* the counter moved since this session's own
/// previous beat, `Heartbeat` otherwise, and the counter is recorded either
/// way. Keeping the comparison here rather than in the owner is what makes it
/// a since-*this*-connection's-last-beat test — sessions beat on their own
/// schedules, so a shared boundary in the owner would let a connection report
/// `Heartbeat` for input that postdates its own previous beat.
public func tick(now: TimeInterval, inputEventCount: UInt64) {
guard status == .connected, !finished, now >= nextHeartbeat else { return }
nextHeartbeat = now + heartbeatInterval
let type: PackageType = config.blockScreenSaver && userActive ? .awake : .heartbeat
let moved = inputEventCount != beatInputEventCount
beatInputEventCount = inputEventCount // Common.cs:558
let type: PackageType = config.blockScreenSaver && moved ? .awake : .heartbeat
originate(broadcast(type))
}